Pseudo code (or Structured English) is used to specify program logic in a (somewhat) English like manner, that aims to be independent of any particular programming language. This simplifies program development by separating it into two main parts, logic design and coding. The use of pseudo code allows the programmer to focus on the logic of the program rather than implementation details such as how data is displayed Once the logic is developed, coding becomes the translation of the pseudo code into the required programming language.
Format variable = expression
Examples
total = 0
y = x * x + z / 4 - 1
name = "Joan Smith"
validLength = True
Format Input variable, variable, ...
Examples
Input custName
Input distance, speed
Format Display value, value, ...
Examples
Display "Hello World"
Display totalPrice, taxPayable
Display "Customer Number: ", custNum, "Name: ", custName
Format IF condition THEN
statement
statement
...
ENDIF
IF condition THEN
statement
statement
...
ELSE
statement
statement
...
ENDIF
IF condition THEN
statements
ELSE
IF condition THEN
statements
ELSE
IF condition THEN
statements
ELSE
statements
ENDIF
ENDIF
ENDIF
IF condition THEN
statements
ELSE IF condition THEN
statements
ELSE IF condition THEN
statements
ELSE
statements
ENDIF
Note: the Else part is optional. Use it where required by the logic.
Examples
IF lineNumber > 50 THEN
Display ""
Display "Student Number", "Student Name"
lineNumber = 0
ENDIF
IF monthNumber >= 1 AND monthNumber <= 12 THEN
Display "valid month"
ELSE
Display "invalid month"
Display "Month must be between 1 and 12"
ENDIF
IF mark >= 80 THEN
grade = "A"
comment = "Excellent"
ELSE
IF mark >= 70 THEN
grade = "B"
ELSE
IF mark >= 60 THEN
grade = "C"
ELSE
IF mark >= 50 THEN
grade = "D"
ELSE
grade = "N"
comment = "Poor"
ENDIF
ENDIF
ENDIF
ENDIF
IF mark >= 80 THEN
grade = "A"
comment = "Excellent"
ELSE IF mark >= 70 THEN
grade = "B"
ELSE IF mark >= 60 THEN
grade = "C"
ELSE IF mark >= 50 THEN
grade = "D"
ELSE
grade = "N"
comment = "Poor"
ENDIF
Format FOR counter = start-value to end-value DO
statement
statement
...
ENDFOR
Example
FOR x = 1 to 10 DO
xSquared = x * x
Display x, xSquared
ENDFOR
Format WHILE condition DO
statement
statement
...
ENDWHILE
Example
count = 1
WHILE count <= 10 DO
Display count
Add 1 to count
ENDWHILE
Format program-name()
statement
statement
...
STOP
Example
addTwoNumbers()
Input number1, number2
sum = number1 + number2
Display sum
STOP
Subroutines are also called procedures or void methods. Subroutines do not return a value.
Format - subroutine calls
subroutine-name()
subroutine-name(parameter, parameter, ...)
Examples - subroutine calls in a program
demoProgram()
...
displayTenStars()
displayAverage(count, total)
STOP
Format - subroutine declarations
subroutine-name()
statement
statement
...
EXIT
subroutine-name(parameter, parameter, ...)
statement
statement
...
EXIT
Examples - subroutine declarations
displayTenStars()
FOR numStars = 1 to 10 DO
Display "*"
ENDFOR
Display ""
EXIT
displayAverage(count, total)
IF count > 0 THEN
average = total / count
Display average
ELSE
Display "Error, nothing to average"
ENDIF
EXIT
Functions are also called non-void methods. Functions return a single value.
Format - function calls
function-name()
function-name(parameter, parameter, ...)
Examples - function calls in a program
demoProgram()
...
month = inputMonth()
IF oddNumber(month) THEN
...
ENDIF
volume = 4 /3 * PI * cube(radius)
max = maximumNumber(a, b)
STOP
Format - function declarations
The following are called functions. They return a single value.
function-name()
statement
statement
...
RETURN with value
function-name(parameter, parameter, ...)
statement
statement
...
RETURN with value
Examples - function declarations
inputMonth()
Input monthNumber
WHILE monthNumber < 1 OR monthNumber > 12 DO
Display "Error invalid month number"
Input monthNumber
ENDWHILE
RETURN with monthNumber oddNumber(num)
oddNumber = false
IF num modulus 2 = 0 THEN
oddNumber = true
ENDIF
RETURN with oddNumber
cube(number)
cube = number * number * number
RETURN with cube
maximumNumber (number1, number2)
IF number1 >= number2 THEN
maximumNumber = number1
ELSE
maximumNumber = number2
ENDIF
RETURN with maximumNumber
The following words have a special meaning in Pseudo Code (in this subject) and should only be used for the purposes described above.
AND, DO, ELSE, ENDIF, ENDFOR, ENDWHILE, EXIT, FOR, IF, NOT, OR, RETURN, STOP, THEN, TO, WHILE.
Other statements: Display, Input
Operators: =, +, -, *, /, (), <, <=, >, >=, []