Pseudocode
This is a simple way to solve a problem in a step wise manner
in English like statement. Algorithm is also sequence of step
and it shows program flow but there is a difference in
algorithm and pseudocode.
Some keywords/ terms used in pseudocode
A) START/BEGIN : This the starting of a pseudocode.
B) END/ STOP : Represents ending of the pseudocode.
C) OUTPUT/ WRITE : To display message/value.
D) INPUT/READ : Accept value from users.
Example of pseudocode
START
OUTPUT “enter a no”
INPUT a
OUTPUT “enter second no”
INPUT b
OUTPUT “sum of two no=”, a+b
END
Syntax in Pseudocode
1. IF . . . ENDIF
2. WHILE . . . ENDWHILE
3. FOR . . . ENDFOR
Simple IF
IF condition THEN
---------
---------
ENDIF
IF ELSE
IF condition THEN
---------
---------
ELSE
---------
---------
ENDIF
NESTED IF
IF condition THEN
---------
---------
ELSEIF condition THEN
---------
---------
ELSEIF condition THEN
---------
---------
ELSE
---------
---------
ENDIF
Examples with IF, IF-ELSE, NESTED-IF
1. Check if a number is positive
BEGIN
INPUT num
IF num > 0 THEN
OUTPUT "The number is positive"
ENDIF
END
2. Check if a number is positive or negative.
BEGIN
INPUT num
IF num > 0 THEN
OUTPUT "The number is positive"
ELSE
OUTPUT "The number is negative or zero"
ENDIF
END
3. Check if a number is positive, negative, or zero.
BEGIN
INPUT num
IF num > 0 THEN
OUTPUT "The number is positive"
ELSE
IF num < 0 THEN
OUTPUT "The number is negative"
ELSE
OUTPUT "The number is zero"
ENDIF
ENDIF
END
Examples with WHILE LOOP
1. Print Numbers from 1 to 10
BEGIN
SET i = 1
WHILE i ≤ 10 DO
OUTPUT (i)
i = i+1
ENDWHILE
END
2. Print Numbers from 1 to 10
BEGIN
INPUT num
SET reverse = 0
WHILE num > 0 DO
remainder = num % 10
reverse = reverse * 10 + remainder
num = num / 10
ENDWHILE
OUTPUT reverse
END
Examples with FOR LOOP
1. Print Numbers from 1 to 10
START
FOR i=1 TO 10 DO
OUTPUT (i)
ENDFOR
END
2. Multiplication Table for a Number
START
INPUT n
FOR i=1 TO 10 DO
OUTPUT (n*i)
ENDFOR
END