SYLLABUS 8.1.4 · PAPER 2 · ALSO FOR 0984 AND 2210

Operators and string handling: DIV, MOD, LENGTH and SUBSTRING

Most programs need to calculate, compare and work with text. This topic covers the operators Cambridge pseudocode uses, and the handful of string operations you need to know.

Arithmetic operators

  • +, −, * and /: add, subtract, multiply and divide. / can give a fractional result: 7 / 2 is 3.5.
  • ^: raise to a power, so 2 ^ 3 is 8.
  • DIV: whole-number division, dropping the remainder. 17 DIV 5 is 3 (also written DIV(17, 5)).
  • MOD: the remainder after division. 17 MOD 5 is 2 (also written MOD(17, 5)).

Comparison and Boolean operators

Comparisons are =, <>, <, >, <= and >=, and each gives TRUE or FALSE. AND needs both parts true, OR needs at least one, and NOT reverses a value. A MOD check is a common test: Number MOD 2 = 0 is TRUE when a number is even.

String handling

  • LENGTH(Word) gives the number of characters.
  • SUBSTRING(Word, Start, Count) gives Count characters beginning at position Start.
  • UCASE(Word) and LCASE(Word) give the text in upper or lower case.
  • Questions say whether the first character is at position 1 or 0. Check before you count.
Name <- "Redpen"
OUTPUT LENGTH(Name)
OUTPUT SUBSTRING(Name, 1, 3)
OUTPUT UCASE(Name)

Where marks go

  • Mixing up DIV and MOD: DIV gives how many times it goes, MOD gives what is left over.
  • Using / when a whole-number answer is needed.
  • Counting string positions from the wrong starting number.
  • Giving SUBSTRING an end position instead of a number of characters.
  • Forgetting that string comparisons care about case unless you use UCASE or LCASE first.

Try a question

4 MARKS · MARKED ON THIS PAGE

A railway timetable program uses these expressions. Write the value of each expression. For strings, write the characters only (no quotation marks are needed).

DIV(47, 6) gives MOD(47, 6) gives LENGTH("Green Park") gives UCASE("bus stop") gives

Fill in your answer first.