Nested IF statements (7c)

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

Nested IF statements (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In a nested IF, when is the inner condition checked?
A Only when the outer condition sends the program into the part that holds the inner IF
B Every time the program runs, before the outer condition
2. Which statement describes a nested IF?
A Two separate IF statements written one after the other
B An IF placed inside the THEN or ELSE part of another IF
3. How many ENDIF lines does this code need? IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager" ENDIF ENDIF
A Two: one for each IF
B One: ENDIF only closes the outer IF
4. Which ENDIF closes the inner IF in a nested IF?
A The first ENDIF, written nearest to the inner IF
B The last ENDIF at the bottom of the code
5. A program checks whether a number is positive. If it is, a second IF checks whether it is even. Where does the second IF go?
A After the ENDIF of the first IF
B Inside the THEN part of the first 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 OUTPUT "Distinction" ENDIF ENDIF AA nested IF: the inner test Mark >= 80 is only checked when the outer test Mark >= 50 is TRUE.
2IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF ENDIF BIF Score > 0 THEN IF Score < 100 THEN OUTPUT "Valid" ENDIF ENDIF
3Every IF, including an inner IF, must be closed by its own ENDIF, and the inner ENDIF comes first. CIF Temp > 30 THEN OUTPUT "Hot" ELSE IF Temp > 15 THEN OUTPUT "Warm" ENDIF ENDIF
4A nested IF can be placed in the ELSE part so that a second condition is only tested if the first is FALSE. DThe outer IF is never closed. The inner ENDIF only closes the inner IF, so a final ENDIF is missing.
5IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF EThe inner IF has no ENDIF of its own. Two IFs need two ENDIFs, so the second ENDIF is missing.
6IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF FAn IF inside the ELSE part: the second test is only checked when Age >= 18 is FALSE.

Definitions

nested IF

Fill in the Blanks

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

1.
IF Age >= 13 THEN __ Age < 20 THEN OUTPUT "Teen"
2.
OUTPUT "Teen" _____ ENDIF
3.
IF Mark >= 80 THEN OUTPUT "Distinction" ____ IF Mark >= 50 THEN OUTPUT "Pass"
4.
IF Age >= 18 THEN IF HasTicket = TRUE ____ OUTPUT "Enter"
5.
ENDIF ENDIF _____

Spot the Error

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

Question 1
A IF Score >= 50 THEN
B IF Score >= 80 THEN
C OUTPUT "Distinction"
D ENDWHILE
E ENDIF
Question 2
A IF Temp > 30 THEN
B IF Temp ← 40 THEN
C OUTPUT "Extreme"
D ENDIF
E ENDIF
Question 3
A IF Age >= 18 THEN
B IF HasTicket = TRUE
C OUTPUT "Enter"
D ENDIF
E ENDIF
Question 4
A INPUT Mark
B IF Mark >= 50 THEN
C IF Mark >= 80 THEN
D OUTPUT Distinction
E ENDIF
F ENDIF
Question 5
A INPUT Age
B IF Age >= 13 THEN
C IF Age >= 19 THEN
D OUTPUT "Teenager"
E ENDIF
F ENDIF

Answer Key

Nested IF statements (7c)
Section A: Quick Questions
Q1
A — Only when the outer condition sends the program into the part that holds the inner IF
Q2
B — An IF placed inside the THEN or ELSE part of another IF
Q3
A — Two: one for each IF
Q4
A — The first ENDIF, written nearest to the inner IF
Q5
B — Inside the THEN part of the first IF
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Distinction" ENDIF ENDIF → A nested IF: the inner test Mark >= 80 is only checked when the outer test Mark >= 50 is TRUE.
Q2
IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF ENDIF → An IF inside the ELSE part: the second test is only checked when Age >= 18 is FALSE.
Q3
Every IF, including an inner IF, must be closed by its own ENDIF, and the inner ENDIF comes first. → IF Score > 0 THEN IF Score < 100 THEN OUTPUT "Valid" ENDIF ENDIF
Q4
A nested IF can be placed in the ELSE part so that a second condition is only tested if the first is FALSE. → IF Temp > 30 THEN OUTPUT "Hot" ELSE IF Temp > 15 THEN OUTPUT "Warm" ENDIF ENDIF
Q5
IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF → The inner IF has no ENDIF of its own. Two IFs need two ENDIFs, so the second ENDIF is missing.
Q6
IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF → The outer IF is never closed. The inner ENDIF only closes the inner IF, so a final ENDIF is missing.
Section C: Definitions
Q1
nested IF — An IF statement placed inside the THEN or ELSE part of another IF, with its own ENDIF. The inner condition is only checked when the outer one sends the program into that part.
Section D: Fill in the Blanks
Q1
IF Age >= 13 THEN IF Age < 20 THEN OUTPUT "Teen"
Q2
OUTPUT "Teen" ENDIF ENDIF
Q3
IF Mark >= 80 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass"
Q4
IF Age >= 18 THEN IF HasTicket = TRUE THEN OUTPUT "Enter"
Q5
ENDIF ENDIF ENDIF
Section E: Spot the Error
Q1
D — The inner IF must be closed with its own ENDIF. ENDWHILE belongs to a WHILE loop. Line 4 should be ENDIF.
Q2
B — The inner IF needs a comparison, not an assignment. ← stores a value, so it should be IF Temp > 40 THEN.
Q3
B — The inner IF is missing THEN. Every IF, nested or not, needs THEN after its condition: IF HasTicket = TRUE THEN.
Q4
D — The text to display must be in double quotes. Without them Distinction looks like a variable. It should be OUTPUT "Distinction".
Q5
C — The inner condition uses the wrong relational operator. Teenagers are 19 or younger, so it should be IF Age <= 19 THEN.