Nested selection: an IF inside an IF

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Nested selection: an IF inside an IF

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A nested IF is checked only when the outer branch it sits inside actually runs. Which statement is correct?
A The inner IF is always checked, whatever the outer IF does
B The inner IF is skipped if its outer branch does not run
2. Which is the correct order for closing two nested IF statements?
A ENDIF (outer) first, then ENDIF (inner)
B ENDIF (inner) first, then ENDIF (outer)
3. IF age >= 16 THEN IF hasId = TRUE THEN OUTPUT "Enter" ENDIF ENDIF A student has age 14. What happens?
A Nothing is output, because the inner IF is never checked
B The inner IF is checked and then fails, so "Enter" is not output
4. How many ENDIF statements does this code need? IF a > 0 THEN IF b > 0 THEN OUTPUT "Both"
A Two: one for each IF
B One: the outer ENDIF covers both IFs
5. Which nested IF is placed inside the ELSE branch of the outer IF?
A IF score >= 50 THEN OUTPUT "Pass" ELSE IF score >= 40 THEN OUTPUT "Near" ENDIF ENDIF
B IF score >= 50 THEN IF score >= 40 THEN OUTPUT "Near" ENDIF ENDIF

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF ENDIF AThe inner IF checks the ticket only when the outer condition Age >= 13 is true, and each IF has its own ENDIF.
2IF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Resit" ENDIF ENDIF BIF Age >= 13 AND HasTicket = TRUE THEN OUTPUT "Enter" ENDIF
3Indentation shows the levels of nesting: the inner IF, its statements and its ENDIF are indented further than the outer IF. CThe ENDIFs are in the wrong order or indentation. The outer ENDIF is written before the inner ENDIF, so the nesting is muddled.
4A nested IF can often be replaced by one IF with AND, when both conditions must be true and there is no ELSE. DThe inner IF is only checked when Score >= 50 is false, so it handles the scores that failed the first test.
5IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF EThe inner IF has no ENDIF of its own. The only ENDIF is used up by one IF, so the other IF is left unclosed.
6IF Score >= 50 THEN IF Score >= 80 THEN OUTPUT "Merit" ENDIF ENDIF FIF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF ENDIF

Definitions

nested IF

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF age >= 16 THEN __ hasTicket = TRUE THEN OUTPUT "Enter"
2.
OUTPUT "Pass" _____ ENDIF
3.
____ IF temp < 10 THEN OUTPUT "Cold"
4.
IF age >= 16 ___ hasId = TRUE THEN OUTPUT "Enter" ENDIF
5.
IF raining = TRUE THEN IF windy = TRUE ____ OUTPUT "Stay in"

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A IF mark >= 50 THEN
B OUTPUT "Pass"
C IF mark => 80 THEN
D OUTPUT "Distinction"
E ENDIF
F ENDIF
Question 2
A IF hasTicket = TRUE THEN
B OUTPUT "Welcome"
C IF age < 12
D OUTPUT "Child seat"
E ENDIF
F ENDIF
Question 3
A IF hasID = TRUE THEN
B IF hasID <- TRUE THEN
C OUTPUT "Can enter"
D ENDIF
E ENDIF
Question 4
A IF age >= 18 DO
B IF hasID = TRUE THEN
C OUTPUT "Entry allowed"
D ENDIF
E ENDIF
Question 5
A IF age >= 13 AND <= 19 THEN
B OUTPUT "Teenager"
C ENDIF

Answer Key

Nested selection: an IF inside an IF
Section A: Quick Questions
Q1
B — The inner IF is skipped if its outer branch does not run
Q2
B — ENDIF (inner) first, then ENDIF (outer)
Q3
A — Nothing is output, because the inner IF is never checked
Q4
A — Two: one for each IF
Q5
A — IF score >= 50 THEN OUTPUT "Pass" ELSE IF score >= 40 THEN OUTPUT "Near" ENDIF ENDIF
Section B: Match the Pairs
Q1
IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF ENDIF → The inner IF checks the ticket only when the outer condition Age >= 13 is true, and each IF has its own ENDIF.
Q2
IF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Resit" ENDIF ENDIF → The inner IF is only checked when Score >= 50 is false, so it handles the scores that failed the first test.
Q3
Indentation shows the levels of nesting: the inner IF, its statements and its ENDIF are indented further than the outer IF. → IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF ENDIF
Q4
A nested IF can often be replaced by one IF with AND, when both conditions must be true and there is no ELSE. → IF Age >= 13 AND HasTicket = TRUE THEN OUTPUT "Enter" ENDIF
Q5
IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF → The inner IF has no ENDIF of its own. The only ENDIF is used up by one IF, so the other IF is left unclosed.
Q6
IF Score >= 50 THEN IF Score >= 80 THEN OUTPUT "Merit" ENDIF ENDIF → The ENDIFs are in the wrong order or indentation. The outer ENDIF is written before the inner ENDIF, so the nesting is muddled.
Section C: Definitions
Q1
nested IF — An IF placed inside the THEN or ELSE branch of another IF. It is only checked when that outer branch runs, and each IF needs its own ENDIF, with the inner ENDIF written first.
Section D: Fill in the Blanks
Q1
IF age >= 16 THEN IF hasTicket = TRUE THEN OUTPUT "Enter"
Q2
OUTPUT "Pass" ENDIF ENDIF
Q3
ELSE IF temp < 10 THEN OUTPUT "Cold"
Q4
IF age >= 16 AND hasId = TRUE THEN OUTPUT "Enter" ENDIF
Q5
IF raining = TRUE THEN IF windy = TRUE THEN OUTPUT "Stay in"
Section E: Spot the Error
Q1
C — The inner IF has the operator written the wrong way round. 'Greater than or equal to' is >=, not =>. It should be IF mark >= 80 THEN.
Q2
C — The inner IF is missing THEN. Every IF, including a nested one, must end its condition with THEN. It should be IF age < 12 THEN.
Q3
B — The inner condition uses <- (assignment) instead of a comparison. A condition must compare values with =. It should be IF hasID = TRUE THEN.
Q4
A — DO belongs to WHILE loops. An outer IF must use THEN to start its branch. It should be IF age >= 18 THEN.
Q5
A — Each side of AND must be a full condition. The second one is missing age. It should be IF age >= 13 AND age <= 19 THEN. This one IF does the work of two nested IFs.