Algorithms- Pseudo Code

Algorithms- Pseudo Code

Understanding Pseudocode

  • Pseudocode is a way of expressing an algorithm in a structured but human-readable format.

  • Pseudocode is not a formal programming language, it uses English language and syntactical conventions from programming.

  • The main aim of pseudocode is clarity. It allows you to comment the general idea of a algorithim before coding it in a specific language.

  • In pseudocode, you may use programming constructs like loops (FOR, WHILE), conditions (IF, ELSE), input/output statements and arrays.

Writing Pseudocode

  • Start by defining the problem, and then decompose it into smaller tasks.

  • Each task can be defined as a sequence, a decision, or an iteration.

  • A sequence is a simple task that is performed and then the flow of control moves on.

  • A decision introduces a question: if the answer is ‘yes’, one action is performed; if ‘no’, another is done.

  • An iteration repeats an action until a condition is met.

  • Use identifiers (names) to refer to data that can be changed, such as variables or arrays.

Examples of Pseudocode

  • Here’s an example of a simple pseudocode to find the larger of two numbers:

      INPUT number_1, number_2
      IF number_1 > number_2 THEN
          OUTPUT number_1
      ELSE
          OUTPUT number_2
      ENDIF
    
  • The above pseudocode accepts two inputs, checks which is larger and outputs the larger number.

Evaluating and Improving Pseudocode

  • Pseudocode should be reviewed and refined for simplicity, clarity and efficiency.

  • Check for any logical errors or ambiguities; make sure the pseudocode does what it’s intended to do.

  • A common way of following through pseudocode is by dry running, which involves tracing through it step by step, tracking variable values to ensure logic works as intended.

  • Flowcharts can also be helpful to visualise the flow of control through the pseudocode.

Adapting Pseudocode to Coding

  • Once pseudocode is refined, use it to guide the development of your program in the specific language you are using.

  • Different languages may need different constructs, but pseudocode establishes the fundamental logic of the solution.

  • Remember that pseudocode isn’t strict; it is a tool for understanding and developing solutions, not the final code itself.