Combining sequence, selection and iteration in one program

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

Combining sequence, selection and iteration in one program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should a total variable be set to 0 in a program that adds up numbers in a loop?
A Inside the loop body
B Before the loop starts
2. Which program order matches sequence, iteration, then output?
A let count = 0; for loop that updates count; console.log(count);
B for loop that updates count; let count = 0; console.log(count);
3. Where should console.log(total) go if you want to print only the final total?
A After the closing } of the loop
B Inside the loop, before the closing }
4. Which code counts how many numbers from 1 to 10 are bigger than 5?
A if (i > 5) { for (let i = 1; i <= 10; i++) { count++; } }
B for (let i = 1; i <= 10; i++) { if (i > 5) { count++; } }
5. Which line sets up a counter correctly before a loop?
A let evens = 0;
B let evens;

Match the Pairs

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

Left columnRight column
1let count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } AThe total is set up after the loop. It must be created and set to 0 before the loop, otherwise the loop uses a variable that does not exist yet and gives an error.
2console.log("Total: " + total); BPrints the final total after the loop has finished, so it only shows the finished answer once
3Set a total to 0 before the loop so it has a starting value to add to Clet total = 0; while (total < 20) { total += 5; }
4An if inside a for loop makes a decision on every repeat, and the result is printed after the loop Dlet big = 0; for (let n = 1; n <= 5; n++) { if (n > 3) { big++; } } console.log(big);
5for (let i = 1; i <= 5; i++) { total += i; } let total = 0; ESets a counter to 0 first, loops through the numbers 1 to 10, and uses an if inside the loop to count only the even numbers
6let count = 0; for (let i = 1; i <= 5; i++) { count++; console.log(count); } FThe console.log is inside the loop, so it prints five times instead of printing the final result once. It should go after the closing brace.

Definitions

sequence, selection, iteration

Fill in the Blanks

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

1.
let total = _; for (let i = 1; i <= 5; i++) { total += i; } console.log(total);
2.
for (let i = 1; i <= 10; i++) { if (i _ 2 === 0) { evens++; } }
3.
let total = 0; for (let i = 1; i <= 3; i++) { total += i; } ___________(total);
4.
let n = 1; let big = 0; while (n <= 5) { if (n > 3) big++; ___; }
5.
___ count = 0; for (let i = 1; i <= 8; i++) { count++; } console.log(count);

Spot the Error

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

Question 1
A let count = 1;
B for (let i = 1; i <= 8; i++) {
C if (i % 2 === 0) count++;
D }
E console.log(count);
Question 2
A const total = 0;
B for (let i = 1; i <= 9; i++) {
C if (i > 5) total += i;
D }
E console.log(total);
Question 3
A let sum = 0;
B for (let i = 1; i <= 5; i++) {
C if i !== 3 sum += i;
D }
E console.log(sum);
Question 4
A let total = 0;
B for (let i = 1; i <= 9; i++) {
C if (i % 2 === 0)
D total = i;
E }
F console.log(total);
Question 5
A let big = 0;
B for (let i = 1, i <= 7, i++) {
C if (i > 4) big++;
D }
E console.log(big);

Answer Key

Combining sequence, selection and iteration in one program
Section A: Quick Questions
Q1
B — Before the loop starts
Q2
A — let count = 0; for loop that updates count; console.log(count);
Q3
A — After the closing } of the loop
Q4
B — for (let i = 1; i <= 10; i++) { if (i > 5) { count++; } }
Q5
A — let evens = 0;
Section B: Match the Pairs
Q1
let count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } → Sets a counter to 0 first, loops through the numbers 1 to 10, and uses an if inside the loop to count only the even numbers
Q2
console.log("Total: " + total); → Prints the final total after the loop has finished, so it only shows the finished answer once
Q3
Set a total to 0 before the loop so it has a starting value to add to → let total = 0; while (total < 20) { total += 5; }
Q4
An if inside a for loop makes a decision on every repeat, and the result is printed after the loop → let big = 0; for (let n = 1; n <= 5; n++) { if (n > 3) { big++; } } console.log(big);
Q5
for (let i = 1; i <= 5; i++) { total += i; } let total = 0; → The total is set up after the loop. It must be created and set to 0 before the loop, otherwise the loop uses a variable that does not exist yet and gives an error.
Q6
let count = 0; for (let i = 1; i <= 5; i++) { count++; console.log(count); } → The console.log is inside the loop, so it prints five times instead of printing the final result once. It should go after the closing brace.
Section C: Definitions
Q1
sequence, selection, iteration — A complete program sets up variables first, repeats with a loop, uses an if inside the loop to make decisions, then prints the result after the loop ends.
Section D: Fill in the Blanks
Q1
let total = 0; for (let i = 1; i <= 5; i++) { total += i; } console.log(total);
Q2
for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { evens++; } }
Q3
let total = 0; for (let i = 1; i <= 3; i++) { total += i; } console.log(total);
Q4
let n = 1; let big = 0; while (n <= 5) { if (n > 3) big++; n++; }
Q5
let count = 0; for (let i = 1; i <= 8; i++) { count++; } console.log(count);
Section E: Spot the Error
Q1
A — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be let count = 0;
Q2
A — A total that changes inside the loop must be declared with let. A const cannot be reassigned, so total += i causes a TypeError. It should be let total = 0;
Q3
C — The condition of an if statement must be inside brackets. It should be if (i !== 3) sum += i;
Q4
D — Using = replaces the total each time instead of adding to it. To build a running total it should be total += i;
Q5
B — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 1; i <= 7; i++) {