Everything from the 4 lessons of this topic on a few pages: the key idea of each lesson, the diagram to remember, the points to learn and the traps examiners set. Read it, cover it, test yourself.
A variable is a named memory location whose value can change while the program runs; declare it with DECLARE Name : TYPE and give it a value with the assignment operator ←. A constant is declared once with CONSTANT Name = value, using a literal only, and can never be changed. Expressions combine values with arithmetic operators (+ - * / DIV MOD), relational operators and the logical operators AND, OR, NOT. Built-in functions and library routines are pre-written, pre-tested subroutines such as LENGTH, MID and INT.
CONSTANT HourlyRate = 12.50.DECLARE Hours : INTEGER and DECLARE Pay : REAL.INPUT Hours then Pay ← Hours * HourlyRate.OUTPUT "Pay: ", Pay. With 6 hours, 6 × 12.50 = 75.00.CONSTANT HourlyRate = 12.50 DECLARE Hours : INTEGER DECLARE Pay : REAL INPUT Hours Pay ← Hours * HourlyRate OUTPUT "Pay: ", PayWith 6 hours the output is Pay: 75.00 (many languages show it as 75.0).
CONSTANT Name = literal, declared at the start, never assigned again.CONSTANT VAT = Price * 0.2 is not allowed: a constant must be a literal. And a constant can never appear on the left of ← later in the program.
17 / 5 is 3.4, not 3. If a question says the result must be an INTEGER, the mark is for DIV (or INT), not /.
In the 9618 guide UCASE and LCASE take a single CHAR, and string positions start at 1. To change a whole string, loop through it with MID one character at a time.
Selection uses IF ... THEN ... ELSE ... ENDIF (which can be nested) or CASE OF ... OTHERWISE ... ENDCASE, whose clauses are tested in order until one matches. Iteration has three forms: a count-controlled loop (FOR ... NEXT) repeats a known number of times; a post-condition loop (REPEAT ... UNTIL) tests at the end, so it always runs at least once; a pre-condition loop (WHILE ... ENDWHILE) tests at the start, so it may run zero times.
Check boundary values. Changing <= to < moves a 2.0 kg parcel into the next price band, and examiners test exactly that value.
CASE stops at the first match. Overlapping ranges are not an error, but every value is caught by the earlier range, so the later clause is dead code.
The UNTIL condition is the condition to stop; the WHILE condition is the condition to keep going. Converting one loop into the other means reversing the condition.
A procedure is a named block of code that performs a task; it may have none, one or more parameters and is run with a CALL statement. A function also performs a task but returns a single value: its header ends RETURNS data type, it uses RETURN, and it is used inside an expression, where the return value replaces the call. The values passed in a call are arguments; the identifiers in the header that receive them are parameters. Subroutines give modular, reusable code that is easier to test and maintain.
CALL ShowBanner() runs ShowBanner, which first calls ShowLine with Length 20 and Symbol '='.CALL ShowLine(5, '*') outputs *****.PROCEDURE Name(...) ... ENDPROCEDURE; run with CALL Name(...).RETURNS type; uses RETURN; used inside an expression, never with CALL.A procedure does not return a value, so X ← ShowLine(5, '*') is wrong. If the main program needs a result back, write a function instead.
CALL CountChar("banana", 'a') loses the mark: a function is never CALLed, and its return value would be thrown away.
Do not mix up parameter and argument. The parameter is the name in the header (Length); the argument is what is passed in the call (4.0).
Passing by value (BYVAL) gives the subroutine a copy of the argument's value: changes to the parameter are local and the caller's variable is unchanged. Passing by reference (BYREF) passes the address of the caller's variable, so the parameter is another name for the same memory location and any change is made to the original. If neither keyword is given, passing by value is assumed. In 9618 pseudocode, parameters should not be passed by reference to a function.
| Score | N | OUTPUT |
|---|---|---|
| 7 | ||
| 7 | ||
| 14 | Inside: 14 | |
| 7 | Outside: 7 |
"The procedure changes N, so Score changes" is the classic wrong answer. With BYVAL the procedure only ever touches its own copy.
In PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER), Y is passed by reference too. Saying Y is passed by value loses the mark.
If a question says a procedure must "return" two values, the answer is BYREF parameters, not two RETURN statements: a procedure has no RETURN value, and a function returns only one value.