Pseudocodes for validation checks (Examples)
OUTPUT "ENTER A NUMBER BETWEEN 1 TO 10"
INPUT Number
WHILE Number<1 OR Number>10 DO
OUTPUT "Invalid, Enter again"
INPUT Number
ENDWHILE
Validate if the number falls within the range of 1 to 10
INPUT NewPassword
IF LENGTH (NewPassword)<8 THEN
OUTPUT "Invalid"
ELSE
OUTPUT "Valid"
ENDIF
Validate if the length of the password is less then 8 characters
OUTPUT "Enter the value "
REPEAT
INPUT Marks
IF Marks <> DIV(Marks, 1) THEN
OUTPUT "Marks must be integer, please re-enter"
ENDIF
UNTIL Marks = DIV(Marks, 1)
ensure that the user inputs an integer value for "Marks"
REPEAT
INPUT Value
UNTIL Value <> NULL AND Value <> ""
Validate if the value is null or blank
Declaration of Variables, Constants and Arrays (Examples)
Variables
DECLARE Marks : INTEGER
DECLARE Name : STRING
DECLARE Average : REAL
DECLARE GameOver : BOOLEAN
Constants
CONSTANT TotalCSTMarks ← 150
CONSTANT DefaultText ← "Enter Here"
Arrays
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE Marks : ARRAY[1:3, 1:3] OF INTEGER
1D and 2D arrays are declared as follows (where 1, 1, 1 are lower bounds and 30, 3, 3 are upper bounds):
Conditional Statements/Selection
Example 1: Movement in Games
INPUT Move
IF Move = 'W' THEN
Position ← Position - 10
ELSE IF Move = 'E' THEN
Position ← Position + 10
ELSE IF Move = 'A' THEN
Position ← Position - 1
ELSE IF Move = 'D' THEN
Position ← Position + 1
ELSE
OUTPUT "Beep"
ENDIF
INPUT Move
CASE OF Move
ꞌWꞌ : Position ← Position – 10
ꞌEꞌ : Position ← Position + 10
ꞌAꞌ : Position ← Position – 1
ꞌDꞌ : Position ← Position + 1
OTHERWISE OUTPUT "Beep"
ENDCASE
Example 2: Finding whether a number is even or odd
INPUT number
IF number MOD 2 = 0 THEN
OUTPUT number, " is an even number"
ELSE
OUTPUT number, " is an odd number"
ENDIF
INPUT number
CASE number MOD 2 OF
0: OUTPUT number, " is an even number"
1: OUTPUT number, " is an odd number"
ENDCASE
Example 3: Student Grades
OUTPUT "Enter score"
INPUT Score
IF Score >= 80 THEN
OUTPUT "A"
ELSEIF Score >= 70 THEN
OUTPUT "B"
ELSEIF Score >= 60 THEN
OUTPUT "C"
ELSEIF Score >= 50 THEN
OUTPUT "D"
ELSE
OUTPUT "U"
ENDIF
OUTPUT "Enter score"
INPUT Score
CASE OF score:
>=80: OUTPUT ("A")
>=70: OUTPUT("B")
>=60: OUTPUT("C")
>=50: OUTPUT ( "D")
OTHERWISE OUTPUT ( "U")
ENDCASE
Loops/Iterations/Repetitions
Example 1: Accepting Name and marks in a One dimensional Array using all three loops
FOR Counter <-- 1 TO 100
INPUT Name[Counter]
INPUT CST_Marks[Counter]
NEXT Counter
Counter <-- 1
WHILE Counter <= 100 DO
INPUT Name[Counter]
INPUT CST_Marks[Counter]
Counter <-- Counter + 1
ENDWHILE
Counter <-- 1
REPEAT
INPUT Name[Counter]
INPUT CST_Marks[Counter]
Counter <-- Counter + 1
UNTIL Counter > 100
Example 2: Rewrite this pseudocode algorithm using a WHILE … DO … ENDWHILE loop (2023 Specimen Paper)
NOTE: This is also an example of Linear Search
B ← FALSE
INPUT Num
FOR Counter ← 1 TO 12
IF A[Counter] = Num THEN
B ← TRUE
ENDIF
NEXT Counter
B ← FALSE
INPUT Num
Counter ← 1
WHILE Counter <= 12 DO
IF A[Counter] = Num THEN
B ← TRUE
ENDIF
Counter ← Counter + 1
ENDWHILE
Displaying a Menu
OUTPUT "Welcome to the Calculator Program"
OUTPUT "Select an option:"
OUTPUT "1. Addition"
OUTPUT "2. Subtraction"
OUTPUT "3. Division"
OUTPUT "4. Multiplication"
OUTPUT "5. Exit"
REPEAT
INPUT choice
IF choice = 1 THEN
OUTPUT "You selected Addition"
ELSE IF choice = 2 THEN
OUTPUT "You selected Subtraction"
ELSE IF choice = 3 THEN
OUTPUT "You selected Division"
ELSE IF choice = 4 THEN
OUTPUT "You selected Multiplication"
ELSE IF choice = 5 THEN
OUTPUT "Exiting the program"
ELSE
OUTPUT "Invalid option. Please select again."
ENDIF
UNTIL choice = 5
OUTPUT "Welcome to the Calculator Program"
OUTPUT "Select an option:"
OUTPUT "1. Addition"
OUTPUT "2. Subtraction"
OUTPUT "3. Division"
OUTPUT "4. Multiplication"
OUTPUT "5. Exit"
REPEAT
INPUT choice
CASE choice OF
1: OUTPUT "You selected Addition"
2: OUTPUT "You selected Subtraction"
3: OUTPUT "You selected Division"
4: OUTPUT "You selected Multiplication"
5: OUTPUT "Exiting the program"
OTHERWISE:
OUTPUT "Invalid option. Please select again."
ENDCASE
UNTIL choice = 5
File Handling (Syntax and Example)
OPENFILE <File identifier> FOR <File mode>
READFILE <File Identifier>, <Variable>
WRITEFILE <File identifier>, <Variable>
CLOSEFILE <File identifier>
DECLARE LineOfText : STRING
OPENFILE FileA.txt FOR READ
OPENFILE FileB.txt FOR WRITE
READFILE FileA.txt, LineOfText
WRITEFILE FileB.txt, LineOfText
CLOSEFILE FileA.txt
CLOSEFILE FileB.txt
Bubble Sort
n = length(arr)
FOR counter1<--1 to n-1
FOR counter2<--1 to n-counter1
IF arr[counter2] > arr[counter2+1]
// Swap process
temp = arr[counter2]
arr[counter2] = arr[counter2+1]
arr[counter2+1] = temp
ENDIF
NEXT
NEXT