Nested IF statements (7a)

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

Nested IF statements (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes a nested IF?
A Two separate IF statements written one after the other
B An IF statement written inside the THEN or ELSE part of another IF
2. When does the inner IF of a nested IF run?
A Only when the outer condition leads to it
B Every time the program runs
3. A program has one IF with another IF inside its THEN part. How many ENDIFs does it need?
A Two, one for each IF
B One, shared by both IFs
4. Which shows correct indentation for a nested IF?
A The inner IF starts at the same position as the outer IF
B The inner IF is indented a further two spaces inside the outer IF
5. Where should the inner ENDIF be written?
A Before the outer ENDIF, lined up with the inner IF
B After the outer ENDIF, lined up with the outer IF

Match the Pairs

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

Left columnRight column
1IF Mark >= 50 THEN IF Mark >= 80 THEN AEach IF has its own ENDIF: the inner one closes first, then the outer one
2 ENDIF ENDIF BThe second IF is nested inside the THEN part, so it is only checked when Mark is at least 50
3A nested IF can be put in the ELSE part, so it is only reached when the outer condition is false CThe outer IF has no ENDIF, because only the inner IF was closed. Each IF needs its own ENDIF.
4Choosing between three outcomes needs a nested IF, with the code indented two more spaces DThe nested IF is not indented by two more spaces, so it is hard to see that it sits inside the ELSE.
5IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Merit" ENDIF EIF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN
6IF Mark >= 50 THEN OUTPUT "Pass" ELSE IF Mark >= 40 THEN OUTPUT "Near" ENDIF ENDIF FIF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Near" ELSE OUTPUT "Fail" ENDIF ENDIF

Definitions

nested IF

Fill in the Blanks

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

1.
IF Score >= 50 THEN __ Score >= 80 THEN OUTPUT "Distinction"
2.
IF Age >= 13 THEN IF Age <= 19 ____ OUTPUT "Teenager"
3.
OUTPUT "Distinction" _____ ENDIF
4.
IF Mark >= 50 THEN OUTPUT "Pass" ____
5.
IF Temp > 0 THEN IF Temp _ 25 THEN OUTPUT "Mild"

Spot the Error

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

Question 1
A IF Mark >= 50 DO
B IF Mark >= 80 THEN
C OUTPUT "Distinction"
D ENDIF
E ENDIF
Question 2
A IF Age >= 13 THEN
B IF HasPermission ← TRUE THEN
C OUTPUT "Welcome"
D ENDIF
E ENDIF
Question 3
A IF Age >= 18 THEN
B IF HasLicence = TRUE THEN
C OUTPUT "Can drive"
D ENDWHILE
E ENDIF
Question 4
A INPUT Mark
B IF Mark >= 50 THEN
C IF Mark => 80 THEN
D OUTPUT "Merit"
E ENDIF
F ENDIF
Question 5
A OUTPUT Age
B IF Age >= 13 THEN
C IF Age <= 19 THEN
D OUTPUT "Teenager"
E ENDIF
F ENDIF

Answer Key

Nested IF statements (7a)
Section A: Quick Questions
Q1
B — An IF statement written inside the THEN or ELSE part of another IF
Q2
A — Only when the outer condition leads to it
Q3
A — Two, one for each IF
Q4
B — The inner IF is indented a further two spaces inside the outer IF
Q5
A — Before the outer ENDIF, lined up with the inner IF
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN IF Mark >= 80 THEN → The second IF is nested inside the THEN part, so it is only checked when Mark is at least 50
Q2
ENDIF ENDIF → Each IF has its own ENDIF: the inner one closes first, then the outer one
Q3
A nested IF can be put in the ELSE part, so it is only reached when the outer condition is false → IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN
Q4
Choosing between three outcomes needs a nested IF, with the code indented two more spaces → IF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Near" ELSE OUTPUT "Fail" ENDIF ENDIF
Q5
IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Merit" ENDIF → The outer IF has no ENDIF, because only the inner IF was closed. Each IF needs its own ENDIF.
Q6
IF Mark >= 50 THEN OUTPUT "Pass" ELSE IF Mark >= 40 THEN OUTPUT "Near" ENDIF ENDIF → The nested IF is not indented by two more spaces, so it is hard to see that it sits inside the ELSE.
Section C: Definitions
Q1
nested IF — An IF statement written inside the THEN or ELSE part of another IF. It only runs when the outer condition leads to it, and each IF needs its own ENDIF.
Section D: Fill in the Blanks
Q1
IF Score >= 50 THEN IF Score >= 80 THEN OUTPUT "Distinction"
Q2
IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager"
Q3
OUTPUT "Distinction" ENDIF ENDIF
Q4
IF Mark >= 50 THEN OUTPUT "Pass" ELSE
Q5
IF Temp > 0 THEN IF Temp < 25 THEN OUTPUT "Mild"
Section E: Spot the Error
Q1
A — An IF statement must use THEN after its condition, not DO. The outer IF should read IF Mark >= 50 THEN.
Q2
B — The ← symbol is for assignment. A condition compares values with =, so the nested IF should read IF HasPermission = TRUE THEN.
Q3
D — Every IF, including a nested one, must be closed with its own ENDIF. ENDWHILE belongs to a WHILE loop, so this line should be ENDIF.
Q4
C — The 'greater than or equal to' operator is >=, not =>. The nested IF should read IF Mark >= 80 THEN.
Q5
A — The program must read the age with INPUT so the IF statements have a value to test. OUTPUT would only display Age, so line 1 should be INPUT Age.