Note that this conventions are not complete rules for writing algorithms. These are the widely accepted conventions for uniformity and clarity among global audience.
-
Comment starts with
//
. -
Statement ends with
;
For block statement, the statement must be enclosed within{ .. }
-
Identifier rules:
- An identifier can only start with letter (A-Z, a-z) or underscore( _ ) symbol.
- An identifier can contain alphabet, digits(0-9), and underscore only.
- The identifier must not contain white spaces.
- A keyword can not use as an identifier.
- You can not use punctuation characters (@, #, $, %, &) as identifers.
- So identifier name should be unique.
-
Assignment Operator ( $:=$ )
It’s same as Walrus operator in Python.int b; int a := 10; b := 20;
``
-
Boolean values (true & false)
Boolean operators ( and / &&, or / ||, not / ! ) -
Array
int a[10]; // for 1D array in Pseudocode and most of others. int a[10][10]; // In C / C++ and most of others. int a[10,10]; // for 2D in Pseudocode.
``
It’s index starts from 0. If explicitly mentioned, the array index starts from the mentioned number, else starts from 0.
// A is an array index starts from 1. A[1] is the first element here. // B is an array. B[0] is the first element here.
``
-
Loops
for, while, repeat until
repeat until
is the substitution fordo...while
.i.
for
loop :for i := 1 to 10 step 1 do { // some work here. }
``
ii.
while
loop :while < condition > do { // some work here. }
``
ii.
repeat ... while
loop :repeat { // some work here. } while < condition >
``
-
Conditionals
if, if ... else, if ... elseif ... else
if <condition> then { ... } // for compound statement.
`
if <condition> then statement. // for single statement.
`
if <condition> then { ... } else { ... } // for else statement.
`
if <condition> then { ... } elseif <condition> then { ... } // for elseif stataement.
`
case { : <condition> : <statement> // other condition here. :else : <statemtent> // for default case. }
`
-
Input and Output
read n; // read variable from user. write n; // stdout n.
``
-
Procedure definition
Algorithm FirstNNaturalNumbers (par1, par2) { }
Algorithm is always titlecase.
Example
An algorithm to find the maximum number among the list of numbers.
Algorithm MaxNumber (arr, n) {
// arr is an array of size n starting from index 0.
// result is a variable to store the max element.
result := arr[0];
for i := 1 to n-1 step 1 do {
if (arr[i] > result) then result := arr[i];
}
write result;
}