Pseudocode cheat sheet
1. What is Pseudocode?
Pseudocode is a simplified, structured way of describing a program's logic without using strict programming language syntax. It focuses on clarity and problem-solving, rather than technicalities.
2. General Rules for Writing Pseudocode
- Be simple and clear: Use plain English combined with programming constructs.
- Consistent indentation: Indent to show hierarchy and structure (e.g., inside loops or conditions).
- Avoid language-specific syntax: Stick to general terms like
OUTPUT
instead ofprint
. - Use capitals for keywords: Make control structures (e.g., IF, ELSE, WHILE) stand out.
3. Common Constructs and Syntax
a. Input and Output
Input from the user:
INPUT variableName
Example:
INPUT userName
Output to the screen:
OUTPUT "Message or value"
Example:
OUTPUT "Hello, World!"
b. Variables and Assignment
- Assign a value to a variable:
Example:variableName ← value
score ← 0
c. Selection (IF Statements)
Basic IF structure:
IF condition THEN
statement(s) ENDIF
Example:
IF score > 50 THEN
OUTPUT "You pass!" ENDIF
IF-ELSE structure:
IF condition THEN
statement(s) ELSE statement(s) ENDIF
Example:
IF age ≥ 18 THEN
OUTPUT "You are an adult." ELSE OUTPUT "You are not an adult." ENDIF
d. Loops
FOR Loop:
FOR variable ← start TO end
statement(s) NEXT variable
Example:
FOR i ← 1 TO 10 OUTPUT i NEXT i
WHILE Loop:
WHILE condition
statement(s) ENDWHILE
Example:
WHILE score < 100
score ← score + 10 ENDWHILE
REPEAT-UNTIL Loop:
REPEAT
statement(s) UNTIL condition
Example:
REPEAT
guess ← INPUT UNTIL guess = answer
e. Procedures and Functions
Procedure (no return value):
PROCEDURE procedureName(parameters)
statement(s) ENDPROCEDURE
Example:
PROCEDURE greet(name)
OUTPUT "Hello, " + name ENDPROCEDURE
Function (with return value):
FUNCTION functionName(parameters)
statement(s) RETURN value ENDFUNCTION
Example:
FUNCTION square(number)
RETURN number * number ENDFUNCTION
f. Arrays
Declaring an array:
arrayName ← [value1, value2, value3]
Example:
scores ← [10, 20, 30]
Accessing array elements:
OUTPUT arrayName[index]
Example:
OUTPUT scores[1] // Outputs 10 (arrays are 0-indexed)
Modifying an array element:
arrayName[index] ← newValue
Example:
scores[2] ← 25
4. Example: Complete Program
Problem: Write pseudocode to calculate the average of 5 numbers entered by the user.
total ← 0
FOR i ← 1 TO 5 OUTPUT "Enter a number:" INPUT num total ← total + num NEXT i average ← total / 5 OUTPUT "The average is:", average
Tips for Exam Success
- Understand the problem: Break it into smaller steps before writing pseudocode.
- Practice regularly: Try converting algorithms into pseudocode.
- Check your work: Ensure your pseudocode follows logical flow and solves the problem.
- Use the marks: Look at how marks are distributed to ensure you've covered all steps.