Pseudo Code Standards Pseudo Code Comman
Pseudo Code Standards Pseudo Code Comman
PART I
How to read the symbols
<> : The angler brackets mean that user must provide the value of whatever is written within these brackets.
[] : The square brackets mean the this is an optional feature. If its not needed, it may be left out.
... : The three dots (ellipses) mean that items similar to previous can come here.
| : A pipe sign shows two or more options (on each side of the sign) either of which can be used.
IF : Anything written in capitol letters is a command and must be written exactly the way it is shown.
' : A single quote is used to show comments about that line of code.
Pseudo-Code Commands
1. Data types
TYPE PURPOSE
Real Floating point numbers, negative or positive. (-143, 0, 45.67, -12.8987 etc.)
String Any character. Can't be used in arithmetic expressions. ("Ali Khan", "14-D/II
Gulberg", "-123", "Hello!?!", "2*7/(3-2)" etc.)
Currency Any negative or positive number with or without decimal. (12343.45, -23, 0,
34333 etc.)
2. Declaring variables
Syntax: DIM <data type> <variable name>
OR
<variable name> : <data type>
OR
<data type> : <variable name>
Note: A variable name (also called an Identifier) must start with either an alphabet or an underscore followed by an
alphabet. It must not contain and special characters such as ^, & @ etc. Variable names should be meaningful, such as
けStudent_Nameげ and not けxげ or けyげ etc. Also, try and keep them short so that they are easier to remember.
3. Using variables
a) Hard coding values into variables
Ex: a = 10
Name= "Ali Khan"
Price = 345.98
Average = 45.77
Total = Price * Quantity 'Total gets the result of Price * Quantity
Ex: Input i
Input Name
Input PartNo, Price, Quantity 'Three values will be asked from the user and put into
these variables respectively
Ex: PRINT i
PRINT "This is a message"
PRINT i + p 'The result of i+p will be printed
PRINT "The result is", i / 2 'Two items will be printed, the message in quotes and
the result of i/2
PRINT ' This PRINT command without anything to print will
actually print an empty line
Note: In some algorithms OUTPUT command is used instead of PRINT. They are both the same as far as pseudo-code is
concern. Just remember not to mix the two. Use only one of them throughout your code.
5. Decisions (branching)
a) Single-line 'IF' statement
Syntax: IF <condition> THEN <statement> [ELSE <statement>]
Note: Only one statement is allowed in both the true and false part. The ELSE part is optional and can be left is nothing is
desired in case the condition is found to be false. Note that there is no END IF in single-line IF condition.
Ex: IF a < 10 THEN PRINT "a is less than 10" ELSE PRINT "a is not less than 10"
Note: Only one path will be taken depending on the evaluation of the condition. The other one will be ignored.
Ex: IF IsWorking = TRUE THEN PRINT "Something is working" 'The ELSE part is optional. Nothing will
be done if the condition in this example
is False
Note: A block 'IF' can be used when more than one statement is required in true and/or false sections. All block 'IF'
structures must be terminated with an ENDIF keyword to establish a clear end of the structure.
Note: Indentation is used to visually separate the IF condition with the nested IF conditions. Otherwise, reading the
nested structures becomes very difficult.
Syntax: FOR <variable> = <start value> TO <end value> [STEP <step value>]
<statements>
NEXT <variable>
Ex: FOR i = 1 TO 10
PRINT "This is line number", i
NEXT i
The STEP clause can be used to change the default increment value of 1.
Usually, the start value is less than or equal to the end value of the counter variable. If the start value is greater than the
end value, then the STEP clause must be used and the step value must be a negative number. It is also possible to use a
variable instead of constants for the start and end values.
Ex: p=1
FOR i = 10 TO p STEP -1
PRINT i
NEXT i
Ex: a=0
DO WHILE a < 10
PRINT a
a=a+1
END DO
Make sure that you increment then counter yourself, otherwise this loop will become an endless loop.
Note: This loop also appears in some algorithms in these forms, however, their working is the same:
Ex: a=0
DO
PRINT a
a=a+1
WHILE a < 10
Unlike the top-testing version, the bottom-testing loop will always run once even if the testing condition is False the first
time around.
Syntax: REPEAT
<statements>
UNTIL <condition>
Ex: a=1
DO UNTIL a > 10
PRINT a
END DO
Unlike the DO WHILE loop, the REPEAT ... UNTIL loops runs 'until' the condition becomes True.
7. Procedures
Syntax: Procedure <procedure name>(parameter list)
<statements>
End Procedure
OR
Procedure HelloWorld
Print "Hello to the World"
End Procedure
8. Functions
Syntax: Function <function name>(parameter list)
<statements>
End Function
OR
Function AMPM
If hours > 1 and hours < 12 then
Return "AM"
Elseif hours > 12 and hours < 24 then
Return "PM"
Endif
End Function
Ex: p = Sum(a,b)
p = a + Sum(d,f)
If sum(a,b) > 10 then Print "sum is greater"
Note: The only difference between a procedure and a function is that a function must return a value to the calling
routine.
END OF PART I