Designing an algorithm that uses logical operators with selection and iteration

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

This Designing an algorithm that uses logical operators with selection and iteration worksheet is part of the ACARA Digital Technologies Years 9–10 — Algorithms with Logical Operators journey. Print it as a booklet?

Designing an algorithm that uses logical operators with selection and iteration

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A login allows 3 attempts. Which loop condition keeps asking while the password is wrong AND attempts are left?
A WHILE password <> correct AND attempts < 3 DO
B WHILE password <> correct OR attempts < 3 DO
2. A game ends when lives = 0 OR score = 10. Which WHILE condition keeps the game running?
A WHILE lives > 0 AND score < 10 DO
B WHILE lives > 0 OR score < 10 DO
3. The same game uses REPEAT...UNTIL. Which line stops the loop correctly?
A UNTIL lives = 0 AND score = 10
B UNTIL lives = 0 OR score = 10
4. Tickets cost $8 for children under 13 or adults 65 and over. Which IF is correct?
A IF age < 13 AND age >= 65 THEN
B IF age < 13 OR age >= 65 THEN
5. A login loop should run while the user is NOT logged in and has attempts left. Which is correct?
A WHILE NOT loggedIn AND attempts < 3 DO
B WHILE loggedIn AND attempts < 3 DO

Match the Pairs

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

Left columnRight column
1WHILE attempts < 3 AND NOT loggedIn DO AIF age < 16 AND day <> "Saturday" THEN price ← 8
2UNTIL lives = 0 OR score = 10 BOR keeps the loop running while attempts are under 3, even after a successful login, or while not logged in, even after 3 failures. It should be AND so both conditions must hold.
3A ticket price decision uses AND so the child price only applies on a weekday when the person is under 16 CAND needs both to be true at once, so the game would not end when only lives = 0 or only score = 10. It should use OR.
4The flowchart and the pseudocode agree when both show the same loop, the same decision and the same AND/OR condition DThe login loop keeps running only while the user has tries left and has not yet logged in. It stops on either event.
5WHILE attempts < 3 OR NOT loggedIn DO EThe game loop stops as soon as either the player runs out of lives or reaches a score of 10.
6IF lives = 0 AND score = 10 THEN OUTPUT "Game over" FREPEAT ... IF lives = 0 OR score = 10 THEN stop

Definitions

WHILE + IF + AND/OR/NOT

Fill in the Blanks

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

1.
attempts ← 0 WHILE attempts < 3 ___ correct = FALSE DO attempts ← attempts + 1
2.
IF age < 13 __ age > 64 THEN price ← 8 ENDIF
3.
REPEAT score ← score + 1 _____ lives = 0 OR score = 10
4.
IF age < 18 AND ___ (day = "Sat") THEN price ← 10 ENDIF
5.
WHILE lives > 0 AND score < 10 DO score ← score + 1 ________

Spot the Error

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

Question 1
A life ← 0
B pts ← 0
C WHILE life > 0 AND pts < 10 DO
D IF hit THEN life ← life - 1
E IF gem THEN pts ← pts + 1
F ENDWHILE
Question 2
A INPUT age, isTue
B price ← 15
C IF age < 13 OR isTue THEN
D price = 8
E ENDIF
F OUTPUT price
Question 3
A n ← 0
B INPUT pw
C WHILE pw <> 1234 OR n < 3 DO
D n ← n + 1
E INPUT pw
F ENDWHILE
Question 4
A INPUT age
B WHILE age < 0 AND age > 120 DO
C OUTPUT 'Try again'
D INPUT age
E ENDWHILE
Question 5
A life ← 3
B pts ← 0
C REPEAT
D IF hit THEN life ← life + 1
E IF gem THEN pts ← pts + 1
F UNTIL life = 0 OR pts = 10

Answer Key

Designing an algorithm that uses logical operators with selection and iteration
Section A: Quick Questions
Q1
A — WHILE password <> correct AND attempts < 3 DO
Q2
A — WHILE lives > 0 AND score < 10 DO
Q3
B — UNTIL lives = 0 OR score = 10
Q4
B — IF age < 13 OR age >= 65 THEN
Q5
A — WHILE NOT loggedIn AND attempts < 3 DO
Section B: Match the Pairs
Q1
WHILE attempts < 3 AND NOT loggedIn DO → The login loop keeps running only while the user has tries left and has not yet logged in. It stops on either event.
Q2
UNTIL lives = 0 OR score = 10 → The game loop stops as soon as either the player runs out of lives or reaches a score of 10.
Q3
A ticket price decision uses AND so the child price only applies on a weekday when the person is under 16 → IF age < 16 AND day <> "Saturday" THEN price ← 8
Q4
The flowchart and the pseudocode agree when both show the same loop, the same decision and the same AND/OR condition → REPEAT ... IF lives = 0 OR score = 10 THEN stop
Q5
WHILE attempts < 3 OR NOT loggedIn DO → OR keeps the loop running while attempts are under 3, even after a successful login, or while not logged in, even after 3 failures. It should be AND so both conditions must hold.
Q6
IF lives = 0 AND score = 10 THEN OUTPUT "Game over" → AND needs both to be true at once, so the game would not end when only lives = 0 or only score = 10. It should use OR.
Section C: Definitions
Q1
WHILE + IF + AND/OR/NOT — A complete design where a loop repeats, IF decisions use AND, OR or NOT, and the flowchart and the pseudocode do the same job, so both versions agree.
Section D: Fill in the Blanks
Q1
attempts ← 0 WHILE attempts < 3 AND correct = FALSE DO attempts ← attempts + 1
Q2
IF age < 13 OR age > 64 THEN price ← 8 ENDIF
Q3
REPEAT score ← score + 1 UNTIL lives = 0 OR score = 10
Q4
IF age < 18 AND NOT (day = "Sat") THEN price ← 10 ENDIF
Q5
WHILE lives > 0 AND score < 10 DO score ← score + 1 ENDWHILE
Section E: Spot the Error
Q1
A — The game should start with 3 lives. With life ← 0 the condition life > 0 is false straight away, so the WHILE loop never runs. It should be life ← 3.
Q2
D — A single = compares two values; it does not store one. To set the discount price the line must use the assignment arrow: price ← 8.
Q3
C — The loop must stop when the password is right OR the attempts run out, so it keeps going only while BOTH pw is wrong AND n < 3. With OR, a wrong password keeps the loop running after 3 tries. It should be AND.
Q4
B — No number can be below 0 and above 120 at the same time, so AND is always false and invalid ages are never re-asked. An invalid age is too low OR too high, so it should be OR.
Q5
D — Being hit should cost a life, so life must go down. Adding 1 means life never reaches 0 and the game cannot end that way. It should be life ← life - 1.