2210 / 0478 · Paper 2

Pseudocode syntax guide

The Cambridge pseudocode layout for every Paper 2 topic.

Open the pseudocode editor

Basics

Output

OUTPUT "Welcome"
OUTPUT "Age: ", Age

Input

OUTPUT "Enter your age"
INPUT Age

Assignment

Total ← 0

Comments

// Comments help people read the code

Data types

INTEGER   // whole number: 25
REAL      // decimal number: 3.75
CHAR      // one character: 'A'
STRING    // text: "Hello"
BOOLEAN   // TRUE or FALSE

Variable declarations

DECLARE Age : INTEGER
DECLARE Price : REAL
DECLARE Grade : CHAR
DECLARE Name : STRING
DECLARE Passed : BOOLEAN
CONSTANT Pi ← 3.142

Arithmetic operators

+    // add
-    // subtract
*    // multiply
/    // divide
^    // power
MOD(10, 3)  // remainder: 1
DIV(10, 3)  // quotient: 3

Relational operators

=    // equal to
<>   // not equal to
<    // less than
<=   // less or equal
>    // greater than
>=   // greater or equal

Boolean operators

AND  // both are true
OR   // either is true
NOT  // reverses it

Selection

IF THEN ENDIF

IF Age >= 18
  THEN
    OUTPUT "Adult"
ENDIF

IF THEN ELSE ENDIF

IF Age >= 18
  THEN
    OUTPUT "Adult"
  ELSE
    OUTPUT "Child"
ENDIF

CASE

CASE OF Choice
  1 : OUTPUT "One"
  2 : OUTPUT "Two"
  OTHERWISE OUTPUT "Error"
ENDCASE

Nested IF

IF Age >= 18
  THEN
    OUTPUT "Adult"
  ELSE
    IF Age >= 13
      THEN
        OUTPUT "Teenager"
      ELSE
        OUTPUT "Child"
    ENDIF
ENDIF

Loops

FOR loop

FOR Count ← 1 TO 10
  OUTPUT Count
NEXT Count

WHILE loop

Count ← 1
WHILE Count <= 10 DO
  OUTPUT Count
  Count ← Count + 1
ENDWHILE

REPEAT loop

Count ← 1
REPEAT
  OUTPUT Count
  Count ← Count + 1
UNTIL Count > 10

FOR loop with STEP

FOR Count ← 2 TO 10 STEP 2
  OUTPUT Count   // 2, 4, 6, 8, 10
NEXT Count

FOR Count ← 10 TO 1 STEP -1
  OUTPUT Count   // 10, 9, ... 1
NEXT Count

Validation and verification

This topic is part of the Paper 2 plan. Basics, Selection and Loops are free for everyone.

See Paper 2 plans Log in

Strings and numbers

This topic is part of the Paper 2 plan. Basics, Selection and Loops are free for everyone.

See Paper 2 plans Log in

Files

This topic is part of the Paper 2 plan. Basics, Selection and Loops are free for everyone.

See Paper 2 plans Log in

Arrays

This topic is part of the Paper 2 plan. Basics, Selection and Loops are free for everyone.

See Paper 2 plans Log in

Subprograms

This topic is part of the Paper 2 plan. Basics, Selection and Loops are free for everyone.

See Paper 2 plans Log in

humaimrancsPrepared by Miss Huma Imran