Sequence: statements run top to bottom

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

Sequence: statements run top to bottom

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does JavaScript do with the statements in a simple program?
A Runs them one after another from top to bottom
B Runs them in a random order
2. Which program prints 5 and then 6?
A let n = 5; n = 6; console.log(n); console.log(n);
B let n = 5; console.log(n); n = 6; console.log(n);
3. True or false: swapping two lines of code can change what a program prints.
A True
B False
4. Which code works without an error?
A let score = 10; console.log(score);
B console.log(score); let score = 10;
5. After this code runs, what does console.log(x) print? let x = 2; x = x + 3; x = x * 2;
A 8
B 10

Match the Pairs

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

Left columnRight column
1let score = 5; score = score + 2; console.log(score); Aconsole.log("Go!"); console.log("Ready");
2console.log("A"); console.log("B"); BRuns top to bottom: score is set to 5, then changed to 7, then 7 is printed
3A variable must be set before it is used, so its line goes above the line that uses it CThe variable lives is used on line 1 before it is set on line 2. The lines are in the wrong order, so this causes an error.
4Swapping two lines changes the output because the lines still run from top to bottom DThe student expects 10 to be printed, but console.log runs before total changes, so it prints 4.
5console.log(lives); let lives = 3; Elet age = 13; console.log(age);
6let total = 4; console.log(total); total = total + 6; FPrints A first and then B, because the first line runs before the second

Definitions

sequence

Fill in the Blanks

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

1.
let score = 5; score = score + 2; console.log(_____);
2.
___ total = 10; console.log(total);
3.
let n = 4; n = n _ 3; console.log(n);
4.
let price = 20; let total = _____ * 2; console.log(total);
5.
let step = 1; step = step + 1; ___________(step);

Spot the Error

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

Question 1
A let a = 4;
B let b = 6;
C let sum = a + b + avg;
D let avg = sum / 2;
E console.log(avg);
Question 2
A let lives = 3;
B lives = live - 1;
C console.log(lives);
Question 3
A let name = "Zoe";
B let msg = "Hi " + msg;
C console.log(msg);
Question 4
A let total = 0;
B total = total + 5;
C total = total + 10;
D total = 10;
Question 5
A let n;
B n = n * 3;
C console.log(n);

Answer Key

Sequence: statements run top to bottom
Section A: Quick Questions
Q1
A — Runs them one after another from top to bottom
Q2
B — let n = 5; console.log(n); n = 6; console.log(n);
Q3
A — True
Q4
A — let score = 10; console.log(score);
Q5
B — 10
Section B: Match the Pairs
Q1
let score = 5; score = score + 2; console.log(score); → Runs top to bottom: score is set to 5, then changed to 7, then 7 is printed
Q2
console.log("A"); console.log("B"); → Prints A first and then B, because the first line runs before the second
Q3
A variable must be set before it is used, so its line goes above the line that uses it → let age = 13; console.log(age);
Q4
Swapping two lines changes the output because the lines still run from top to bottom → console.log("Go!"); console.log("Ready");
Q5
console.log(lives); let lives = 3; → The variable lives is used on line 1 before it is set on line 2. The lines are in the wrong order, so this causes an error.
Q6
let total = 4; console.log(total); total = total + 6; → The student expects 10 to be printed, but console.log runs before total changes, so it prints 4.
Section C: Definitions
Q1
sequence — Statements run one after another from top to bottom, so changing the order of lines can change the result. A variable must be set before it is used.
Section D: Fill in the Blanks
Q1
let score = 5; score = score + 2; console.log(score);
Q2
let total = 10; console.log(total);
Q3
let n = 4; n = n * 3; console.log(n);
Q4
let price = 20; let total = price * 2; console.log(total);
Q5
let step = 1; step = step + 1; console.log(step);
Section E: Spot the Error
Q1
C — Line 3 uses avg, but avg is not set until line 4. Statements run top to bottom, so avg does not exist yet. It should be let sum = a + b;
Q2
B — The name live was never set, so line 2 causes a ReferenceError. The variable set on line 1 is lives, so it should be lives = lives - 1;
Q3
B — Line 2 uses msg while it is still being created, so it has no value yet. It should join "Hi " with name, which was set on line 1.
Q4
D — Line 4 is missing here as line 3: each step must build on the last. total = 10 replaces the value and throws away the 5 from line 2. It should be total = total + 10; so the output is 15.
Q5
A — Line 1 gives n no value, so when line 2 runs n is undefined and n * 3 gives NaN. A variable must be set before it is used: let n = 2;