Choosing nested IFs or one compound condition

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

Choosing nested IFs or one compound condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. One action should happen only when BOTH tests are true. Which line matches IF age >= 13 THEN IF hasPermission = TRUE THEN ... (with no ELSE)?
A IF age >= 13 AND hasPermission = TRUE THEN
B IF age >= 13 OR hasPermission = TRUE THEN
2. A game gives a badge when score > 50 and time < 60. Nothing happens otherwise. Which is the better choice?
A One IF with AND, because there is one action and no separate ELSE
B Nested IFs, because there are two tests
3. A door system must show 'Invalid card' if the card is bad, and 'Wrong PIN' if the card is good but the PIN is wrong. Which structure fits?
A IF card = valid AND pin = correct with one ELSE
B Nested IF, so each test gets its own ELSE message
4. True or false: IF A AND B THEN action ENDIF gives the same result as IF A THEN IF B THEN action ENDIF ENDIF.
A True, when the action needs both tests and there are no separate ELSE actions
B False, because the two forms always behave differently
5. IF A AND B THEN ... ELSE ... ENDIF. When does the ELSE part run?
A Only when both A and B are false
B When A is false, or B is false, or both are false

Match the Pairs

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

Left columnRight column
1IF score >= 50 AND attended = TRUE THEN DISPLAY "Pass" ENDIF AThe one ELSE covers both failures, so a person who is old enough but has no licence is wrongly told "Too young".
2IF raining THEN IF windy THEN DISPLAY "Stay in" ELSE DISPLAY "Umbrella" ENDIF ELSE DISPLAY "Go out" ENDIF BOne action needs both tests, so a single compound condition does the job.
3Two nested IFs with the same single action can be rewritten as one IF with AND CNesting is not needed here. There is only one action and no separate ELSE outcomes, so the code is harder to read than necessary.
4When failing each test needs a different message, nest the IFs with separate ELSE parts DIF A THEN IF B THEN DISPLAY "Yes" ENDIF ENDIF is the same as IF A AND B THEN DISPLAY "Yes" ENDIF
5IF age >= 16 AND hasLicence THEN DISPLAY "Drive" ELSE DISPLAY "Too young" ENDIF EIF age >= 16 THEN IF hasLicence THEN DISPLAY "Drive" ELSE DISPLAY "Get licence" ENDIF ELSE DISPLAY "Too young" ENDIF
6IF member = TRUE THEN IF spend > 50 THEN DISPLAY "Discount" ENDIF ENDIF FNesting gives each test its own ELSE outcome, which one AND condition cannot do.

Definitions

nested IF vs AND

Fill in the Blanks

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

1.
IF score >= 50 ___ attendance >= 80 THEN OUTPUT "Pass" ENDIF
2.
IF score >= 50 THEN __ attendance >= 80 THEN OUTPUT "Pass"
3.
IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ____ OUTPUT "Low attendance"
4.
IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ENDIF _____
5.
IF hasTicket = TRUE ___ age >= 18 THEN OUTPUT "Enter" ENDIF

Spot the Error

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

Question 1
A IF age > 12 OR age < 18 THEN
B OUTPUT "Teen ticket"
C ENDIF
Question 2
A INPUT paid
B INPUT age
C IF paid AND age < 18
D OUTPUT "Youth entry"
E ENDIF
Question 3
A IF score >= 50 THEN
B IF attend >= 90 THEN
C OUTPUT "Pass"
D ENDWHILE
E ENDIF
Question 4
A IF ticket = TRUE THEN
B IF age < 18 THEN
C OUTPUT "Adult entry"
D ENDIF
E ENDIF
Question 5
A IF score <= 50 AND hw THEN
B OUTPUT "Pass"
C ENDIF

Answer Key

Choosing nested IFs or one compound condition
Section A: Quick Questions
Q1
A — IF age >= 13 AND hasPermission = TRUE THEN
Q2
A — One IF with AND, because there is one action and no separate ELSE
Q3
B — Nested IF, so each test gets its own ELSE message
Q4
A — True, when the action needs both tests and there are no separate ELSE actions
Q5
B — When A is false, or B is false, or both are false
Section B: Match the Pairs
Q1
IF score >= 50 AND attended = TRUE THEN DISPLAY "Pass" ENDIF → One action needs both tests, so a single compound condition does the job.
Q2
IF raining THEN IF windy THEN DISPLAY "Stay in" ELSE DISPLAY "Umbrella" ENDIF ELSE DISPLAY "Go out" ENDIF → Nesting gives each test its own ELSE outcome, which one AND condition cannot do.
Q3
Two nested IFs with the same single action can be rewritten as one IF with AND → IF A THEN IF B THEN DISPLAY "Yes" ENDIF ENDIF is the same as IF A AND B THEN DISPLAY "Yes" ENDIF
Q4
When failing each test needs a different message, nest the IFs with separate ELSE parts → IF age >= 16 THEN IF hasLicence THEN DISPLAY "Drive" ELSE DISPLAY "Get licence" ENDIF ELSE DISPLAY "Too young" ENDIF
Q5
IF age >= 16 AND hasLicence THEN DISPLAY "Drive" ELSE DISPLAY "Too young" ENDIF → The one ELSE covers both failures, so a person who is old enough but has no licence is wrongly told "Too young".
Q6
IF member = TRUE THEN IF spend > 50 THEN DISPLAY "Discount" ENDIF ENDIF → Nesting is not needed here. There is only one action and no separate ELSE outcomes, so the code is harder to read than necessary.
Section C: Definitions
Q1
nested IF vs AND — IF A AND B gives the same result as IF A THEN IF B when one action needs both tests. Use nesting when each test needs its own different ELSE outcome.
Section D: Fill in the Blanks
Q1
IF score >= 50 AND attendance >= 80 THEN OUTPUT "Pass" ENDIF
Q2
IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass"
Q3
IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ELSE OUTPUT "Low attendance"
Q4
IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ENDIF ENDIF
Q5
IF hasTicket = TRUE AND age >= 18 THEN OUTPUT "Enter" ENDIF
Section E: Spot the Error
Q1
A — Both tests must be true for a teen ticket, so the tests need AND. With OR, every age passes at least one test. It should be IF age > 12 AND age < 18 THEN.
Q2
C — An IF with a compound condition still needs THEN at the end of the line. It should be IF paid AND age < 18 THEN.
Q3
D — The inner IF must be closed with ENDIF, not ENDWHILE, which belongs to a WHILE loop. Each nested IF needs its own ENDIF.
Q4
C — The output runs only when ticket is TRUE and age < 18, so it must say youth entry. The message "Adult entry" does not match the tests. It should be OUTPUT "Youth entry".
Q5
A — The student needs a score of 50 or more to pass, so the operator must be >=. With <= the low scores pass and the high scores fail. It should be IF score >= 50 AND hw THEN.