A loop inside an if block

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

A loop inside an if block

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A loop is written inside an if block. When does the loop run?
A Every time the algorithm runs
B Only when the if condition is true
2. score <- 3 if score > 5 then for i from 1 to 4 print i What is printed?
A 1 2 3 4
B Nothing, the condition is false so the loop is skipped
3. x <- 2 if x > 0 then for i from 1 to x print i What is printed?
A 1 then 2
B Nothing
4. Which algorithm only repeats when the answer is 'yes'?
A if answer = "yes" then for i from 1 to 3 print "Hi"
B for i from 1 to 3 print "Hi" if answer = "yes" then
5. An else branch can hold a loop too.
A True
B False

Match the Pairs

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

Left columnRight column
1if score > 10 then for i from 1 to 3 print i AThe loop is inside the else branch, so it only runs when the if condition was false
2else while lives > 0 lives <- lives - 1 BThe loop is not indented under then, so it is outside the if block and runs no matter what the condition is
3If the condition is false, the loop in the true branch is skipped completely CThe loop is inside the true branch, so it only runs when score is bigger than 10
4A loop can sit in the else branch and run only when the condition is false Dif x > 0 then for i from 1 to x print i
5if x > 0 then print "Positive" for i from 1 to x print i EThe loop is in the else branch, but x is only positive in the true branch, so the loop will never run properly
6if x > 0 then print "Positive" else for i from 1 to x print i Fif age >= 13 then print "Teen" else for i from 1 to 3 print "Kid"

Definitions

if x > 0 then for i from 1 to x

Fill in the Blanks

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

1.
if score > 50 then ___ i from 1 to 3 print "Pass"
2.
if x > 0 then for i from 1 to _ print i
3.
____ for i from 1 to 3 print "Low"
4.
if age __ 13 then for i from 1 to 3 print "Hi"
5.
if x > 0 then for i from 1 to x print _

Spot the Error

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

Question 1
A x <- 3
B if x > 0 then
C for i from 1 to x
D print j
E end for
F end if
Question 2
A if score >= 50 then
B print "Pass"
C for i from 1 3
D print "Well done"
E end for
F end if
Question 3
A when n > 0 then
B for i from 1 to n
C print i
D end for
E end if
Question 4
A if n > 0 then
B for i from 1 to n
C print i
D end if
E end if
Question 5
A if n > 0 then print n
B else
C for i from 1 to n
D print "Low"
E end for
F end if

Answer Key

A loop inside an if block
Section A: Quick Questions
Q1
B — Only when the if condition is true
Q2
B — Nothing, the condition is false so the loop is skipped
Q3
A — 1 then 2
Q4
A — if answer = "yes" then for i from 1 to 3 print "Hi"
Q5
A — True
Section B: Match the Pairs
Q1
if score > 10 then for i from 1 to 3 print i → The loop is inside the true branch, so it only runs when score is bigger than 10
Q2
else while lives > 0 lives <- lives - 1 → The loop is inside the else branch, so it only runs when the if condition was false
Q3
If the condition is false, the loop in the true branch is skipped completely → if x > 0 then for i from 1 to x print i
Q4
A loop can sit in the else branch and run only when the condition is false → if age >= 13 then print "Teen" else for i from 1 to 3 print "Kid"
Q5
if x > 0 then print "Positive" for i from 1 to x print i → The loop is not indented under then, so it is outside the if block and runs no matter what the condition is
Q6
if x > 0 then print "Positive" else for i from 1 to x print i → The loop is in the else branch, but x is only positive in the true branch, so the loop will never run properly
Section C: Definitions
Q1
if x > 0 then for i from 1 to x — A loop written inside an if branch only runs when that branch's condition is true. If the condition is false, the loop is skipped. An else branch can hold a loop too.
Section D: Fill in the Blanks
Q1
if score > 50 then for i from 1 to 3 print "Pass"
Q2
if x > 0 then for i from 1 to x print i
Q3
else for i from 1 to 3 print "Low"
Q4
if age >= 13 then for i from 1 to 3 print "Hi"
Q5
if x > 0 then for i from 1 to x print i
Section E: Spot the Error
Q1
D — The loop counter is i, not j. The variable j was never created, so the loop inside the if block can't print it. It should be print i.
Q2
C — A for loop needs the word to between the start and end values. The loop inside the if block should read for i from 1 to 3.
Q3
A — A selection block starts with the keyword if, not when. The loop only runs if this condition is true, so the line should be if n > 0 then.
Q4
D — The loop is closed with end for, not end if. The loop must be closed first, then the if block is closed with end if on the last line.
Q5
C — The else branch only runs when n is 0 or less. A loop from 1 to n would be skipped even when the else branch runs. It should be for i from 1 to 3.