PSEUDOCODE
PSEUDOCODE:
Pseudocode is a way of describing the steps of an algorithm using a mixture of plain English and simple
programming-like structures.
It is not a programming language, it doesn’t follow the exact rules of a real language, but it is structured so that both
humans and programmers can easily understand how the solution will work before actually coding it.
KEY POINTS ABOUT PSEUDOCODE:
Readable: Written in a form that is easy for humans to follow.
Language-independent: Not tied to any specific programming language.
Structured: Uses common programming concepts like variables, loops, and conditions.
Purpose: Helps plan and design algorithms before writing actual code, and makes logic easier to debug.
EXAMPLE:
Problem: Find the sum of the first 5 positive integers.
total ← 0
FOR i ← 1 TO 5
total ← total + i
NEXT i
OUTPUT total
Font: Monospaced (e.g. Courier New).
Indentation: 3 spaces for nested blocks.
Keywords: UPPER CASE (e.g. IF, REPEAT).
Identifiers: MixedCase or PascalCase (e.g. NumberOfPlayers).
Comments: Start with // (each line of a multi-line comment starts with //).
Line Numbers: Only when required; not part of code.
VARIABLES, CONSTANTS, AND DATA TYPES:
DATA TYPES (KEYWORDS USED IN PSEUDOCODE):
INTEGER – whole numbers
REAL – numbers with decimal
CHAR – single character in single quotes 'A'
STRING – sequence of characters in double quotes "Hello"
BOOLEAN – TRUE / FALSE
DATE – format dd/mm/yyyy
LITERALS:
Integer: 5, -3
Real: 4.7, 0.3
Char: 'X'
String: "Text"
Boolean: TRUE / FALSE
IDENTIFIERS:
Start with letter; can have letters, digits, _.
Case-insensitive.
Use descriptive names.
DECLARATIONS:
DECLARE Counter : INTEGER
DECLARE Name : STRING
CONSTANTS:
CONSTANT Pi = 3.14159
Only literal values allowed.
ASSIGNMENTS:
Total ← Price * Quantity
Operator for assignment: ←
COMMON OPERATIONS:
I/O:
INPUT Name
OUTPUT "Hello ", Name
ARITHMETIC OPERATORS:
+, -, *, / (always gives REAL)
DIV – integer division
MOD – remainder
RELATIONAL OPERATORS:
>, <, >=, <=, =, <> (result is BOOLEAN)
LOGIC OPERATORS:
AND, OR, NOT
STRING FUNCTIONS:
RIGHT(S, x)
LENGTH(S)
MID(S, start, length)
LCASE(c) / UCASE(c)
Concatenate: "Hi" & " " & "There"
NUMERIC FUNCTIONS:
INT(x) – integer part
RAND(x) – random real between 0 and x (excl. x)
VARIABLE, INPUT/ OUTPUT AND DATATYPE
TASK A. Calculate the radius of a sphere
1. Get the radius of a sphere from the user.
2. Calculate the volume of the sphere.
3. Output value to the console.
Pseudocode:
DECLARE Volume, Radius: REAL
INPUT Radius
Volume <- (4/3)*3.14*Radius
OUTPUT Volume
TASK B. Calculate the perimeter of a rectangle
1. Get the length and width of a rectangle from the user.
2. Calculate the perimeter of the rectangle.
3. Output the value to the console.
Pseudocode:
DECLARE Length, Width, Perimeter: REAL
INPUT Length
INPUT Width
Perimeter <- 2 * (Length + Width)
OUTPUT Perimeter
TASK C. Convert distance from kilometers to miles
1. Get the distance in kilometers from the user.
2. Convert the distance to miles (1 km = 0.621371 miles).
3. Output the value to the console.
Pseudocode:
DECLARE Kilometers, Miles: REAL
INPUT Kilometers
Miles <- Kilometers * 0.621371
OUTPUT Miles
TASK D. Calculate the area of a rectangle
1. Get the length and width of a rectangle from the user.
2. Calculate the area of the rectangle.
3. Output the value to the console.
CONDITIONALS
IF–ELSE:
IF statements may or may not have an ELSE clause.
IF statements without an else clause are written as follows:
IF <condition> THEN
<statement(s)>
ENDIF
TASK E. A program reads the temperature. If the temperature is above 30, display “It’s hot”
Pseudocode:
DECLARE Temperature: REAL
INPUT Temperature
IF Temperature > 30 THEN
OUTPUT "It's hot"
ENDIF
IF statements with an else clause are written as follows:
IF <condition> THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
Note, due to space constraints, the THEN and ELSE clauses may only be indented by two spaces rather than three.
(They are, in a sense, a continuation of the IF statement rather than separate statements).
TASK F. A program checks if a number is positive or negative. If the number is zero, treat it as positive
Pseudocode:
DECLARE Number: REAL
INPUT Number
IF Number >= 0 THEN
OUTPUT "Positive"
ELSE
OUTPUT "Negative"
ENDIF
NESTED IF STATEMENTS:
A nested IF statement is simply an IF statement written inside another IF (or ELSE) block. It’s used when you need to
check an additional condition only if the first condition is true (or false). Think of it like: “If condition A is true, then
check condition B.”
IF <condition1> THEN
IF <condition2> THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
ELSE
IF <condition3> THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
ENDIF
TASK G. A program checks if a person is eligible to vote
1. If the person’s age is 18 or above, check if they are a citizen.
2. If they are a citizen, output “Eligible to vote”.
3. If not, output “Not eligible to vote”.
4. If the age is less than 18, output “Underage”.
Pseudocode:
DECLARE Age: INTEGER
DECLARE IsCitizen: BOOLEAN
INPUT Age
INPUT IsCitizen
IF Age >= 18 THEN
IF IsCitizen = TRUE THEN
OUTPUT "Eligible to vote"
ELSE
OUTPUT "Not eligible to vote"
ENDIF
ELSE
OUTPUT "Underage"
ENDIF
TASK H. Write a program that takes two numbers and an operator (+, -, *, /) from the user
1. If the operator is valid, perform the calculation.
2. If the operator is /, check if the second number is not zero before dividing.
3. If the operator is invalid, display “Invalid operation”.
Pseudocode:
DECLARE Num1, Num2, Result: REAL
DECLARE Operator: CHAR
INPUT Num1
INPUT Num2
INPUT Operator
IF Operator = '+' THEN
Result ← Num1 + Num2
OUTPUT Result
ELSE
IF Operator = '-' THEN
Result ← Num1 - Num2
OUTPUT Result
ELSE
IF Operator = '*' THEN
Result ← Num1 * Num2
OUTPUT Result
ELSE
IF Operator = '/' THEN
IF Num2 ≠ 0 THEN
Result ← Num1 / Num2
OUTPUT Result
ELSE
OUTPUT "Error: Division by zero"
ENDIF
ELSE
OUTPUT "Invalid operation"
ENDIF
ENDIF
ENDIF
ENDIF
CASE-OF:
CASE-OF statements (also called switch statements in some programming languages) are a type of multi-way
selection structure. They allow a program to compare a single variable (or expression) against several possible values
and execute different code depending on which value matches, instead of writing multiple IF ... ELSE IF ... ELSE blocks.
Basic Structure:
CASE OF <identifier>
<value1> : <statement1>
<statement2>
...
<value2> : <statement1>
<statement2>
...
...
ENDCASE
Using OTHERWISE: An OTHERWISE clause can be included as the last case to handle situations when none of the
preceding cases match.
CASE OF <identifier>
<value1> : <statement1>
<statement2>
<value2> : <statement1>
<statement2>
...
OTHERWISE : <statement1>
<statement2>
ENDCASE
Using Ranges: A case value can represent a range of values:
<value1> TO <value2> : <statement1>
<statement2>
TASK I. Write a program that asks the user to enter a grade (A, B, C, D, or F) and displays the corresponding
feedback:
A → "Excellent"
B → "Good"
C → "Average"
D → "Needs improvement"
F → "Fail"
If the grade entered is not one of these, display "Invalid grade".
Pseudocode:
DECLARE Grade: CHAR
INPUT Grade
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Average"
'D' : OUTPUT "Needs improvement"
'F' : OUTPUT "Fail"
OTHERWISE : OUTPUT "Invalid grade"
ENDCASE
TASK J. Write a program that asks the user to enter a day number (1–7) and displays the corresponding day of the
week. If the number is not between 1 and 7, display "Invalid day".