Pseudocode is a hybrid language that combines English & code. It is composed of 3 main structures
- Sequence
- Selection
- Iteration
Sequence
Sequence refers to the the order of processing in pseudocode. Processes go from top to bottom, always.
Selection
If door is locked then:
Unlock door
Else if windows is open then:
Close Windows
End if
Selection is the process of making a decision from a given conditional statement, also referred to as a boolean statement. it includes:
- If statements
- Else Statements
- Match/Switch Statements
Iteration
While alive do:
Breather
End do:
For i from 1 to 10 do:
Display i
End do
For each element n in list do:
Display n
End do
Iteration is when a process repeats itself, or loops, until some condition is met. Usually, it includes:
- either a boolean being a specific value or a counter variable (such as i) iterating through a range of values.
- Iterating if we are unsure when the conditional is true
Variables
#Decleration
x <- Integer
G <- Graph
#Initialization
x <- 0
G <- Empty Graph
#Operations
x <- x + 1
Add node to G
Other ‘Syntax’
- Defining a *helper *function:
Function nameOfFunction(params):
- Defining an algorithm:
Algorithm nameOfAlgorithm(params):
- ADT Operation -
Enqueue elem to Queue
- Return a value:
Return value
- Print:
Display (string)
Examples
- Basic sum of squares:
Algorithm Mean(list):
sum ← 0
n ← len(list)
For i from 1 to n:
sum ← sum + list[i]
return sum/n