IGCSE 0478 Pseudocode Crash Course
IGCSE 0478 Pseudocode Crash Course
Syntax Essentials
Core Structures
• Pitfall: Don’t omit ENDCASE , and use OTHERWISE as the final case for “else”. Each case should
be a single statement or a simple block.
1
FOR i ← start TO end [STEP increment]
<statements>
NEXT
This runs i from start up to end (inclusive) 8 . The default step is +1, or specify STEP k for
increments/decrements 9 .
• Pitfall: Always include NEXT (optionally followed by the loop variable for clarity). If start >
end , the loop body does not execute. Watch off-by-one errors (the loop includes both bounds).
WHILE <condition> DO
<statements>
ENDWHILE
The condition is tested first; if true, the block executes and repeats until false 10 .
• Pitfall: Ensure the loop variable/condition changes inside the loop, or you’ll get an infinite loop
(or no execution if the condition is false initially).
REPEAT
<statements>
UNTIL <condition>
The body runs at least once, then repeats until <condition> is true 11 .
• Pitfall: Don’t forget UNTIL <condition> . If you need to loop until a password or valid input,
use REPEAT…UNTIL .
2
OUTPUT "Enter password"
UNTIL password = "secret"
• Pitfall: Always declare arrays with their size before use. You cannot assign to a whole slice at
once; use a loop to initialize many elements (e.g. FOR i ← 1 TO 10: A[i] ← 0 ). Remember
array bounds to avoid index errors.
• Strings: Use built-in string functions. For example, LENGTH(s) returns the length of string s
16 . To extract substrings, use something like SUBSTRING(s, start, count) (Cambridge
docs call this MID(s, start, count) 17 ). First character is position 1.
• Pitfall: Off-by-one is common (remember the first character is index 1). Calling SUBSTRING with
too large a range or negative indices should be avoided unless specified.
• Procedures: A procedure does not return a value. Define with PROCEDURE and call with CALL
18 19 . Example:
PROCEDURE PrintHello()
OUTPUT "Hello!"
ENDPROCEDURE
CALL PrintHello()
• Pitfall: Don’t use RETURN in a procedure. Always end the block with ENDPROCEDURE . Use
CALL to invoke it.
3
FUNCTION Max(A : INTEGER, B : INTEGER) RETURNS INTEGER
IF A > B THEN
RETURN A
ELSE
RETURN B
ENDIF
ENDFUNCTION
Result ← Max(7, 10) // Result = 10
• Pitfall: Never forget the RETURN statement (and its data type). Unlike procedures, do not write
CALL Max(...) ; instead use Result ← Max(...) .
Functions vs Procedures: The key difference is that functions return a value (and must be declared with
RETURNS <type> ), whereas procedures do not 20 . Use functions for calculations (so you can assign
their result) and procedures for actions (like printing or swapping).
• Parameters (BYVAL vs BYREF): By default, parameters are passed by value (the function/
procedure gets a copy) 22 . To pass by reference (allowing the routine to modify the caller’s
variable), prefix the parameter with BYREF in the definition 23 . Example:
a ← 3, b ← 5
CALL Swap(a, b)
// Now a = 5 and b = 3
• Pitfall: If you want to change the caller’s variable, you must use BYREF . Omitting BYREF
means the original will not change. (Also, do not use BYREF with functions in Cambridge
pseudocode 22 .)
// Example calls
CALL DisplayRange(1, 10)
Value ← Square(5) // Value = 25
4
Practice Problem
Exercise: Write pseudocode to input an integer and check if it lies between 1 and 100 (inclusive). If it
does, output "In range" , otherwise output "Out of range" . Use an IF…THEN…ELSE structure
and OUTPUT.
Hint: You will need to INPUT the number, use a condition like IF num >= 1 AND num <= 100
THEN ... ELSE ... , and OUTPUT the appropriate message.
1 2 3 4 5 6 7 8 9 10 11 12 14 askpakchairul.files.wordpress.com
https://askpakchairul.files.wordpress.com/2015/05/0478_pseudocode_guide.pdf