An if statement inside a loop

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

An if statement inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where does the if go so it is checked on every repeat of the loop?
A After the loop's closing brace }
B Inside the loop's curly braces { }
2. for (let i = 1; i <= 5; i++) { if (i > 3) { console.log(i); } } What does this print?
A 4 then 5
B 1 2 3 4 5
3. Which condition checks if the loop counter i is an even number?
A if (i % 2 === 0)
B if (i % 2 = 0)
4. let count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } What is count at the end?
A 10
B 5
5. To count only the matches in a loop, where should count++ go?
A Inside the if, so it only runs when the test is true
B Inside the loop but outside the if

Match the Pairs

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

Left columnRight column
1for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } Afor (let i = 0; i < scores.length; i++) { if (scores[i] > 50) { passes++; } }
2for (let i = 1; i <= 5; i++) { if (i > 3) { console.log(i); } else { console.log("low"); } } BPrints low for 1, 2 and 3, then prints the number for 4 and 5, because each pass takes a different branch.
3Count only the scores that are above 50 by putting an if inside the loop. CChecks every number from 1 to 10 and adds 1 to count only when the number is even.
4Use % 2 inside a loop to tell whether each number is even. DThe if uses = (assignment) instead of === (comparison), so it does not test whether i is even. It should be i % 2 === 0.
5for (let i = 1; i <= 10; i++) { if (i % 2 = 0) { count++; } } Efor (let n = 1; n <= 8; n++) { if (n % 2 === 0) { console.log(n); } }
6if (i % 2 === 0) { for (let i = 1; i <= 10; i++) { count++; } } FThe if is outside the loop, so it is only checked once and every number gets counted. The if must go inside the loop body.

Definitions

for (...) { if (...) { ... } }

Fill in the Blanks

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

1.
for (let n = 1; n <= 6; n++) { __ (n > 3) { console.log(n); } }
2.
for (let i = 1; i <= 10; i++) { if (i _ 2 === 0) { console.log(i); } }
3.
let count = 0; for (let n = 1; n <= 9; n++) { if (n > 5) { _____++; } }
4.
for (let i = 1; i <= 4; i++) { if (i > 2) { console.log("big"); } ____ { console.log("small"); } }
5.
let total = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { total += _; } }

Spot the Error

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

Question 1
A let matches = 0;
B for (let n = 1; n <= 8; n++) {
C if (n % 2 === 0) {
D matches + 1;
E }
F }
Question 2
A for (let i = 1; i <= 6; i++) {
B if (i % 2 = 0) {
C console.log(i);
D }
E }
Question 3
A let total = 0;
B for (let i = 0; i < 10, i++) {
C if (i > 5) {
D total += i;
E }
F }
Question 4
A for (let i = 1; i <= 5; i++ {
B if (i % 2 === 1) {
C console.log("odd " + i);
D }
E }
Question 5
A for (let i = 1; i <= 4; i++) {
B if (i % 2 === 0) {
C console.log("even);
D } else {
E console.log("odd");
F }

Answer Key

An if statement inside a loop
Section A: Quick Questions
Q1
B — Inside the loop's curly braces { }
Q2
A — 4 then 5
Q3
A — if (i % 2 === 0)
Q4
B — 5
Q5
A — Inside the if, so it only runs when the test is true
Section B: Match the Pairs
Q1
for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } → Checks every number from 1 to 10 and adds 1 to count only when the number is even.
Q2
for (let i = 1; i <= 5; i++) { if (i > 3) { console.log(i); } else { console.log("low"); } } → Prints low for 1, 2 and 3, then prints the number for 4 and 5, because each pass takes a different branch.
Q3
Count only the scores that are above 50 by putting an if inside the loop. → for (let i = 0; i < scores.length; i++) { if (scores[i] > 50) { passes++; } }
Q4
Use % 2 inside a loop to tell whether each number is even. → for (let n = 1; n <= 8; n++) { if (n % 2 === 0) { console.log(n); } }
Q5
for (let i = 1; i <= 10; i++) { if (i % 2 = 0) { count++; } } → The if uses = (assignment) instead of === (comparison), so it does not test whether i is even. It should be i % 2 === 0.
Q6
if (i % 2 === 0) { for (let i = 1; i <= 10; i++) { count++; } } → The if is outside the loop, so it is only checked once and every number gets counted. The if must go inside the loop body.
Section C: Definitions
Q1
for (...) { if (...) { ... } } — An if inside a loop body is checked again on every repeat, so each pass can take a different branch, such as counting only the matches.
Section D: Fill in the Blanks
Q1
for (let n = 1; n <= 6; n++) { if (n > 3) { console.log(n); } }
Q2
for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { console.log(i); } }
Q3
let count = 0; for (let n = 1; n <= 9; n++) { if (n > 5) { count++; } }
Q4
for (let i = 1; i <= 4; i++) { if (i > 2) { console.log("big"); } else { console.log("small"); } }
Q5
let total = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { total += i; } }
Section E: Spot the Error
Q1
D — matches + 1; works out a value but never stores it, so the counter stays at 0. It should be matches++; to add 1 each time the if is true.
Q2
B — A single = assigns a value instead of comparing. The condition should use === so each pass checks whether i is even: if (i % 2 === 0).
Q3
B — The parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 0; i < 10; i++) {.
Q4
A — The closing bracket of the for loop header is missing. It should be for (let i = 1; i <= 5; i++) {.
Q5
C — The string is missing its closing quote. It should be console.log("even"); so the loop can print even on the passes where the if is true.