← ACARA Digital Technologies Years 9–10

Choosing nested IFs or one compound condition

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.

Understanding Choosing nested IFs or one compound condition in ACARA Digital Technologies Years 9–10

What it does: The action only runs when A and B are both true, so both versions give the same result. Each ELSE tells the user which test failed. A single AND condition has only one ELSE, so it cannot do this. When the tests share one action, the compound condition is shorter and easier to read. It also avoids extra ENDIFs and indentation. The two failures need different messages. Nesting lets each IF have its own ELSE, so the right message appears for the right reason. There are three different outcomes. The inner IF needs its own ELSE, so the tests must be nested.

Common mistakes: 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. An IF with a compound condition still needs THEN at the end of the line. It should be IF paid AND age < 18 THEN.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 492–534) and word search (Modules 459–534).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
One action should happen only when BOTH tests are true. Which line matches IF age >= 13 THEN IF hasPermission = TRUE THEN ... (with no ELSE)?
IF age >= 13 AND hasPermission = TRUE THENIF age >= 13 OR hasPermission = TRUE THEN
Matches
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".
Applying
Meanings
Term
nested IF vs AND
Meaning
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.
Practice
IF age >= 13 THEN
Testing
Gaps
IF score >= 50 attendance >= 80 THEN
Check Answer
Spots
1IF age > 12 OR age < 18 THEN
2 OUTPUT "Teen ticket"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Desert Jerboa' badge — High Jumper, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
A nested IF with no ELSE runs its action only when both tests pass. That is exactly what AND does. OR would run the action when only one test passes.
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
Having two tests does not mean you need nesting. With one action and no different outcomes, a single compound condition gives the same result and is shorter.
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 ✓
Each test needs its own different outcome, so nesting is needed. One ELSE after an AND cannot tell you which test failed.
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
With one action that needs both tests, the two forms behave the same. They only differ when you add ELSE actions for each test.
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 ✓
AND is true only when both tests are true. So the ELSE runs whenever at least one test fails. This is why one ELSE cannot show which test failed.

Match the Pairs

IF score >= 50 AND attended = TRUE THEN DISPLAY "Pass" ENDIF → One action needs both tests, so a single compound condition does the job.
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.
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
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
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".
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.

Key Term

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.

Fill in the Blanks

1. IF score >= 50 AND attendance >= 80 THEN OUTPUT "Pass" ENDIF
Hint: Type the logical operator that makes one action need both tests to be true.
2. IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass"
Hint: Type the keyword that starts a second decision inside the first, so the action needs both tests.
3. IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ELSE OUTPUT "Low attendance"
Hint: Type the keyword that gives the inner test its own different outcome when it fails.
4. IF score >= 50 THEN IF attendance >= 80 THEN OUTPUT "Pass" ENDIF ENDIF
Hint: Type the keyword that closes the outer decision, since each nested IF needs its own ending.
5. IF hasTicket = TRUE AND age >= 18 THEN OUTPUT "Enter" ENDIF
Hint: Type the logical operator that replaces a nested IF when both tests lead to the same single action.

Spot the Error

Question 1
A IF age > 12 OR age < 18 THEN
B OUTPUT "Teen ticket"
C ENDIF
Line 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.
Question 2
A INPUT paid
B INPUT age
C IF paid AND age < 18
D OUTPUT "Youth entry"
E ENDIF
Line 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.
Question 3
A IF score >= 50 THEN
B IF attend >= 90 THEN
C OUTPUT "Pass"
D ENDWHILE
E ENDIF
Line D — The inner IF must be closed with ENDIF, not ENDWHILE, which belongs to a WHILE loop. Each nested IF needs its own ENDIF.
Question 4
A IF ticket = TRUE THEN
B IF age < 18 THEN
C OUTPUT "Adult entry"
D ENDIF
E ENDIF
Line 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".
Question 5
A IF score <= 50 AND hw THEN
B OUTPUT "Pass"
C ENDIF
Line 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.

Student with a class code? Join your class →