Computer Science
Pseudocode

Pseudocode is so named to reflect the fact that although it is like code, it is not actual programming code. Pseudo means false.

The syntax of pseudocode is less strict than the syntax of a programming language - there is no compiler or debugger telling you that you have not placed a semi-colon at the end of the line.

There are lots of versions of pseudocode and no single standard.

We tend to write a little more detail in pseudocode than we do in Structured English. It is, perhaps, a little closer to the code we may write in our programming language.

Sequence

As before, statements are written in the order in which they need to be executed. Look carefully at the example to see how input, output and assignment are handled in pseudocode.

Input Hours
Input Rate
Wage ← Hours * Rate
Output wage

Selection

If hours > 40
   THEN
      overtime ← hours - 40
      pay ← pay + overtime * 3.75
EndIf
If average > 50
   THEN Output "Pass"
   ELSE Output "Fail"
EndIf
If x In [0 ... 9, A, B, C, D, E, F]
   THEN Output "Valid Hex"
   ELSE Output "Bad Hex"
EndIf
Case x Of
   1: Output "January"
   2: Output "February"
   3: Output "March"
EndCase

Iteration

For x ← 1 To 12
   Output x
EndFor

This executes the statement, Output x 12 times. The variable x is called a stepper variable. Each time the loop executes, the value of x increases by 1. The output from this algorithm would be the numbers 1 to 12.

Repeat
   x ← x + 1
Until x = 6

In this example, the statement within the construct will be executed at least once. If the code prior to that shown in the example makes x have a value that cannot reach six by repeatedly adding 1, the loop will continue indefinitely or until an overflow occurs because these is not enough memory to store the value of x. The loop executes at least once.

While x < 6 Do
   x ← x + 1
EndWhile

In this example, the value of x is examined before executing the statements contained within the loop. The statement will not execute if, at this point in the program, x has a value of 6 or greater.

For Each x In [5, 7, 9]
   Output x
EndFor

From Pseudocode To C#

The following pseudocode describes an algorithm for adding two numbers together and reporting their sum and average.

Input Number1
Input Number2
Sum ← Number1 + Number2
Average ← Sum / 2
Output Sum, Average

When we write this in C#, we get the following,

double number1;
double number2;
double sum;
double average;
Console.Write("Input Number 1: ");
number1 = System.Convert.ToDouble(Console.ReadLine());
Console.Write("Input Number 2: ");
number2 = System.Convert.ToDouble(Console.ReadLine());
sum = number1 + number2;
average = sum / 2;
Console.WriteLine("Sum: {0}, Average: {1}", sum, average);
Console.ReadLine();

There is quite a difference. When we write an algorithm, we are more concerned with whether we are doing the right thing to achieve the result than we are with the exact form of the statements that we need in a programming language. As a result, our actual code is longer and more fussy. Ultimately, it is easier to see if the logic of the algorithm is correct in the shorter, more concise form offered by pseudocode.