For Loops

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

This For Loops worksheet is part of the JavaScript Coding without HTML and CSS journey. Print it as a booklet?

For Loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does a for loop have three parts separated by semicolons?
A Yes
B No
2. Must 'for' be written in lowercase?
A No
B Yes
3. Does i++ increase i by 1 after each pass?
A No
B Yes
4. Can a for loop count down using i--?
A Yes
B No
5. Are the three parts of a for loop separated by commas?
A No
B Yes

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 <= 3; i++) { ... } AWrong - the parts of a for loop use semicolons, not commas
2for (let j = 5; j > 0; j--) { ... } Bi++
3Sets the loop counter's starting value CRuns the block 3 times, with i going from 1 to 3
4Increases i by 1 after each pass through the loop Dlet i = 1
5for let i = 1; i <= 3; i++ { ... } ECounts down from 5 to 1, decreasing j each pass
6for (let i = 1, i <= 3; i++) { ... } FWrong - the loop header needs parentheses

Definitions

for

Fill in the Blanks

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

1.
___ (let i = 1; i <= 3; i++) {
2.
for (let i = 1; i __ 3; i++) {
3.
for (let i = 1; i <= 3; i__) {
4.
for (let j = 5; j > 0; j__) {
5.
for (___ k = 0; k < 4; k++) {

Spot the Error

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

Question 1
A For (let i = 1; i <= 3; i++) {
B console.log(i);
C }
Question 2
A console.log(i);
B for let i = 1; i <= 3; i++ {
C }
Question 3
A let total = 0;
B console.log(total);
C for (let i = 1, i <= 3; i++) {
Question 4
A console.log(i);
B console.log(x);
C console.log(y);
D for (let i = 1; i <= 3; i++) [
Question 5
A for (let i = 0; i < 5; i++) {
B console.log(i)
C }

Answer Key

For Loops
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
B — Yes
Q4
A — Yes
Q5
A — No
Section B: Match the Pairs
Q1
for (let i = 1; i <= 3; i++) { ... } → Runs the block 3 times, with i going from 1 to 3
Q2
for (let j = 5; j > 0; j--) { ... } → Counts down from 5 to 1, decreasing j each pass
Q3
Sets the loop counter's starting value → let i = 1
Q4
Increases i by 1 after each pass through the loop → i++
Q5
for let i = 1; i <= 3; i++ { ... } → Wrong - the loop header needs parentheses
Q6
for (let i = 1, i <= 3; i++) { ... } → Wrong - the parts of a for loop use semicolons, not commas
Section C: Definitions
Q1
for — Repeats a block of code a set number of times, using an initializer, condition, and update expression.
Section D: Fill in the Blanks
Q1
for (let i = 1; i <= 3; i++) {
Q2
for (let i = 1; i <= 3; i++) {
Q3
for (let i = 1; i <= 3; i++) {
Q4
for (let j = 5; j > 0; j--) {
Q5
for (let k = 0; k < 4; k++) {
Section E: Spot the Error
Q1
A — 'for' must be lowercase - a capital F causes an error.
Q2
B — The loop header needs parentheses: for (...).
Q3
C — The three parts of a for loop are separated by semicolons, not commas.
Q4
D — Code blocks use curly braces {}, not square brackets [].
Q5
B — This line is missing a semicolon, but that's not required in JS.