Detailed · Deep study
1Variables, constants and data types
A named memory location that stores a value which can change while the program is running.
A named memory location that stores a value which cannot change while the program is running.
| Data type | Stores | Example |
|---|---|---|
| INTEGER | a whole number | 42, −3 |
| REAL | a number with a fractional part | 4.7, −0.5 |
| CHAR | a single character | 'A', '@' |
| STRING | a sequence of zero or more characters | "Hello", "" |
| BOOLEAN | TRUE or FALSE | TRUE |
DECLARE Counter : INTEGER
DECLARE Price : REAL
DECLARE Initial : CHAR
DECLARE FirstName : STRING
DECLARE GameOver : BOOLEAN
CONSTANT VAT ← 0.2
Counter ← 0
Price ← 12.50
GameOver ← FALSE2Input and output
OUTPUT "Enter your name"
INPUT FirstName
OUTPUT "Hello ", FirstName, ", you have ", Lives, " lives left"3Sequence, selection and iteration
Sequence: statements are carried out one after another, in order. The order matters – e.g. a total must be set to 0 before values are added.
Selection: a decision chooses which statements run. Use IF for conditions and ranges; use CASE to choose between several single values.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFINPUT Choice
CASE OF Choice
1 : OUTPUT "New game"
2 : OUTPUT "Load game"
3 : OUTPUT "Quit"
OTHERWISE OUTPUT "Invalid choice"
ENDCASEIteration: statements are repeated.
| Loop | Pseudocode | Use when |
|---|---|---|
| count-controlled | FOR … TO … NEXT | the number of repeats is known |
| pre-condition | WHILE … DO … ENDWHILE | repeats while a condition is true; tested first, so may run 0 times |
| post-condition | REPEAT … UNTIL | repeats until a condition is true; tested at the end, so runs at least once (e.g. validation) |
FOR Count ← 1 TO 10
OUTPUT Count * 5
NEXT Count
FOR Count ← 10 TO 0 STEP -2
OUTPUT Count
NEXT CountINPUT Number
WHILE Number <> -1 DO
Total ← Total + Number
INPUT Number
ENDWHILEREPEAT
OUTPUT "Enter the password"
INPUT Password
UNTIL Password = "Secret"4Totalling and counting
Total ← 0
Count ← 0
FOR Index ← 1 TO 20
INPUT Temperature
Total ← Total + Temperature // totalling
IF Temperature > 30
THEN
Count ← Count + 1 // counting
ENDIF
NEXT Index
OUTPUT "Total ", Total, "; days above 30: ", Count5String handling
| Operation | Returns |
|---|---|
| LENGTH(String) | the number of characters |
| SUBSTRING(String, Start, Length) | part of the string, starting at position Start |
| UCASE(String) | the string in upper case |
| LCASE(String) | the string in lower case |
Word ← "Computer"
OUTPUT LENGTH(Word) // 8
OUTPUT SUBSTRING(Word, 1, 4) // "Comp"
OUTPUT UCASE(Word) // "COMPUTER"
OUTPUT LCASE(Word) // "computer"6Operators
| Type | Operators |
|---|---|
| arithmetic | + − * / ^ (raised to the power of) MOD (remainder) DIV (whole-number division) |
| relational | = < <= > >= <> (not equal to) – the result is always TRUE or FALSE |
| logical | AND (both true) OR (at least one true) NOT (reverses) |
Worked example
Evaluate expressions
Find the values of DIV(23, 4), MOD(23, 4), 2 ^ 3 and (5 > 3) AND NOT (2 = 2).
- 1
23 ÷ 4 = 5 remainder 3.
DIV gives 5, MOD gives 3
- 2
2 × 2 × 2.
8
- 3
TRUE AND NOT TRUE = TRUE AND FALSE.
FALSE
Answer5, 3, 8, FALSE
7Nested statements
A nested statement is one inside another: selection inside a loop, a loop inside a loop, or an IF inside an IF. You will not have to write more than three levels.
FOR Row ← 1 TO 3
FOR Column ← 1 TO 4
IF Row = Column
THEN
OUTPUT "*"
ELSE
OUTPUT "-"
ENDIF
NEXT Column
NEXT Row8Procedures, functions and parameters
A named block of code (subroutine) that carries out a task; it is called by name and does not return a value.
A named block of code (subroutine) that carries out a task and returns a single value to the point where it was called.
A value passed into a procedure or function when it is called (up to three in exam questions).
PROCEDURE Stars(Number : INTEGER)
DECLARE Count : INTEGER // local variable
FOR Count ← 1 TO Number
OUTPUT "*"
NEXT Count
ENDPROCEDURE
FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL
RETURN Length * Width
ENDFUNCTION
CALL Stars(5)
OUTPUT "Area = ", Area(4.5, 2.0)| Local variable | Global variable | |
|---|---|---|
| declared | inside a procedure or function | in the main program, outside any subroutine |
| scope | can only be used in that subroutine | can be used anywhere in the program |
| lifetime | exists only while the subroutine runs | exists while the whole program runs |
Local variables avoid accidental changes from other parts of the program and save memory; subroutines can be reused and tested separately.
9Library routines
A library routine is a pre-written, tested subroutine built into the language that can be used in any program.
| Routine | Returns |
|---|---|
| MOD(a, b) | the remainder when a is divided by b |
| DIV(a, b) | the whole-number quotient, with the fractional part discarded |
| ROUND(x, places) | x rounded to the given number of decimal places |
| RANDOM() | a random number between 0 and 1 inclusive |
OUTPUT DIV(17, 5) // 3 (whole-number quotient)
OUTPUT MOD(17, 5) // 2 (remainder)
OUTPUT ROUND(3.14159, 2) // 3.14
Dice ← ROUND(RANDOM() * 5, 0) + 1 // whole number from 1 to 610Maintainable programs
A maintainable program is easy for another programmer to understand, correct and update. It uses: meaningful identifiers for variables, constants, arrays, procedures and functions (e.g. TotalCost, not T); comments to explain what the code does; and procedures and functions to split the code into manageable parts.
// Calculates the total cost of an order, including VAT
CONSTANT VATRate ← 0.2
DECLARE NetCost : REAL
DECLARE TotalCost : REAL
INPUT NetCost
TotalCost ← NetCost * (1 + VATRate) // add VAT
OUTPUT "Total cost: ", ROUND(TotalCost, 2)| Pseudocode | Python |
|---|---|
| DECLARE Count : INTEGER | count = 0 (no declarations) |
| X ← 5 | x = 5 |
| INPUT Name | name = input() |
| OUTPUT "Hi ", Name | print("Hi", name) |
| IF … THEN … ELSE … ENDIF | if …: / else: |
| FOR I ← 1 TO 10 … NEXT I | for i in range(1, 11): |
| WHILE … DO … ENDWHILE | while …: |
| MOD(a, b) / DIV(a, b) | a % b / a // b |
11Exam focus
Key words: variable; constant; data type; sequence; selection; iteration; count-controlled; pre-condition; post-condition; totalling; counting; string handling; operator; nested; procedure; function; parameter; local; global; library routine; maintainable.
Quick self-check
Answer in your head first, then reveal.
INTEGER (1); REAL (1); BOOLEAN (1).
Pre-condition tests the condition before the loop so it may not run (1); post-condition tests at the end so it runs at least once (1).
"xam" (1).
A function returns a value (1); a procedure does not / is called with CALL (1).
Any two: meaningful identifiers; comments; procedures/functions; constants (2).
