Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Selection: IF and CASE statements

Practise 35 exam-style questions on Selection: IF and CASE statements for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 35 questions
  • 1–6 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

A program shows a menu with six options, numbered 1 to 6, and the user inputs the number of their choice.

Explain why a CASE statement is more suitable than a series of IF statements for processing the choice.

Model answer

The choice is a single variable that has to be matched against six fixed values, which is what CASE is designed for. Each option goes on its own line, so it is much shorter and easier to read than six nested IFs, and OTHERWISE deals with any invalid number.

Explanation

Both structures are selection, but CASE suits one variable with many separate values (menus, codes, single letters), while IF suits conditions such as ranges or combinations with AND and OR. Six nested IFs would also break Cambridge's guideline of nesting no more than three levels deep.

Mark scheme
  1. 1 mark

    A CASE statement compares one variable with a list of specific values, which is exactly what a menu choice needs

    max 2; 1 mark for each point

  2. 1 mark

    It is shorter / clearer / easier to read and maintain than many nested IF statements

  3. 1 mark

    OTHERWISE handles any invalid choice in one place

  4. 1 mark

    It avoids deep nesting and repeating the variable in every condition

Question 2

2 marks

Look at this pseudocode.

IF Age >= 17
  THEN
    IF HasLicence = TRUE
      THEN
        OUTPUT "You may drive"
    ENDIF
ENDIF

Rewrite this pseudocode as a single IF statement that produces the same output.

Model answer

IF Age >= 17 AND HasLicence = TRUE
  THEN
    OUTPUT "You may drive"
ENDIF

Explanation

The inner IF is only reached when the outer condition is TRUE, and the output only happens when the inner condition is also TRUE - which is exactly what AND means. Combining them removes a level of nesting and an ENDIF, making the code easier to read.

Mark scheme
  1. 1 mark

    MP1 one IF statement with the two conditions joined by AND

    do not accept OR

  2. 1 mark

    MP2 complete and correct: IF Age >= 17 AND HasLicence = TRUE THEN OUTPUT "You may drive" ENDIF

Question 3

3 marks

A traffic light controller stores its current colour in the variable Light as a character: 'R' (red), 'A' (amber) or 'G' (green).

Write pseudocode that uses a CASE statement to output "Stop" for 'R', "Get ready" for 'A', "Go" for 'G', and "Fault" for any other character.

Model answer

CASE OF Light
  'R' : OUTPUT "Stop"
  'A' : OUTPUT "Get ready"
  'G' : OUTPUT "Go"
  OTHERWISE OUTPUT "Fault"
ENDCASE

Explanation

Each CASE option is a value, a colon and the statement to run for that value. The values are characters, so they are written in single quotes; without quotes, R, A and G would be treated as variable names. OTHERWISE catches any unexpected value, such as a fault in the sensor.

Mark scheme
  1. 1 mark

    MP1 CASE OF Light ... ENDCASE

  2. 1 mark

    MP2 'R', 'A' and 'G' each with the correct output, written as characters in single quotes

    accept double quotes around the case values

  3. 1 mark

    MP3 OTHERWISE OUTPUT "Fault"

Question 4

4 marks

Write pseudocode that inputs the lengths of the three sides of a triangle and outputs:
- "Equilateral" if all three sides are equal
- "Isosceles" if exactly two sides are equal
- "Scalene" if no two sides are equal.

Model answer

INPUT Side1
INPUT Side2
INPUT Side3
IF Side1 = Side2 AND Side2 = Side3
  THEN
    OUTPUT "Equilateral"
  ELSE
    IF Side1 = Side2 OR Side2 = Side3 OR Side1 = Side3
      THEN
        OUTPUT "Isosceles"
      ELSE
        OUTPUT "Scalene"
    ENDIF
ENDIF

Explanation

Order matters: an equilateral triangle also has two equal sides, so it must be tested first, and the isosceles test is placed in its ELSE. The isosceles test needs all three pairs joined by OR - checking only Side1 = Side2 would miss triangles such as 5, 7, 5.

Mark scheme
  1. 1 mark

    MP1 inputs the three side lengths

  2. 1 mark

    MP2 equilateral test: Side1 = Side2 AND Side2 = Side3

  3. 1 mark

    MP3 isosceles test: any two sides equal (Side1 = Side2 OR Side2 = Side3 OR Side1 = Side3), tested only when the triangle is not equilateral

    all three pairs must be checked

  4. 1 mark

    MP4 outputs Scalene otherwise, with exactly one message output

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.