0984

Programming Concepts

Programming · 4 question types

Exam Frequency Analysis

Past paper frequency (2018 to 2024)

This topic accounts for approximately 13% of your exam marks.

stable
High
Stable13%

Paper 2 is almost entirely programming. Variable types, assignments, loops and conditionals are tested every session.

Logical operators combine Boolean values and return a Boolean. The three you must know are AND, OR and NOT.

OperatorMeaningResult
ANDResult is TRUE only when every combined condition is also TRUE(Age >= 13) AND (Age <= 18) is true only for ages in that range
ORTrue if at least one condition is true(Score > 90) OR (BonusActive = TRUE) is true if either holds
NOTFlips the result of one condition (true becomes false, and the other way around)NOT GameOver is true while the game is still going

Truth tables

The three operators are fully defined by their truth tables (these come up again in topics 26 and 27 on Boolean logic).

AND

ABA AND B
FALSEFALSEFALSE
FALSETRUEFALSE
TRUEFALSEFALSE
TRUETRUETRUE

OR

ABA OR B
FALSEFALSEFALSE
FALSETRUETRUE
TRUEFALSETRUE
TRUETRUETRUE

NOT

ANOT A
FALSETRUE
TRUEFALSE

Combining comparisons and logical operators

A single comparison only tests one thing. To check more than one fact at once, link comparisons with AND, OR or NOT. Each part on either side of the operator must be its own complete comparison.

AND requires both sides to be true. A common pattern is a range check built from two comparisons:

IF (Mark >= 0) AND (Mark <= 100)
   THEN
      OUTPUT "Valid mark"
ENDIF

OR requires at least one side to be true. Useful when several different values should be treated the same way:

IF (Day = "Saturday") OR (Day = "Sunday")
   THEN
      OUTPUT "Weekend"
ENDIF

NOT flips the result of a single comparison, so it is true when the underlying condition is false:

IF NOT (Account = "locked")
   THEN
      OUTPUT "Login allowed"
ENDIF

Always bracket each comparison. The brackets are not strictly required by precedence, but they make the intent obvious and stop slips like writing Mark >= 0 AND <= 100, which is not valid pseudocode.

Operator precedence

When multiple operators appear in one expression:

  1. Brackets first.
  2. Then arithmetic (*, / before +, -); DIV(a, b) and MOD(a, b) are function calls, so they are worked out first.
  3. Then comparisons (<, >, =, <>, <=, >=).
  4. Then NOT.
  5. Then AND.
  6. Then OR.

When in doubt, use brackets generously. (A AND B) OR C is much clearer than relying on the reader to remember precedence.