While Loops

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

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

While Loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does a while loop repeat as long as its condition is true?
A Yes
B No
2. Must 'while' be written in lowercase?
A No
B Yes
3. Does a while loop's condition need to be in parentheses?
A Yes
B No
4. Can forgetting to update a variable cause an infinite loop?
A Yes
B No
5. Does a while loop's code block use curly braces?
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
1while (count <= 3) { ... } AWrong - use a comparison operator, not = for assignment
2while (isRunning) { ... } BInfinite loop
3Updates a variable so the loop eventually stops Ccount = count + 1;
4Runs forever because the condition never becomes false DWrong - the condition needs parentheses
5while n > 0 { n = n - 1; } ERepeats the block while isRunning stays true
6while (z = 3) { z = z + 1; } FRepeats the block while count is 3 or less

Definitions

while

Fill in the Blanks

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

1.
_____ (count <= 3) {
2.
while (count __ 3) {
3.
while (n > 0) { n = n _ 1; }
4.
_____ (isRunning) {
5.
while (i < 5) { i = i _ 1; }

Spot the Error

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

Question 1
A let count = 1;
B Wile (count <= 3) {
C count = count + 1;
Question 2
A while n > 0 {
B let n = 5;
C n = n - 1;
Question 3
A let i = 0;
B while (i < 5) {
C console.log(i)
Question 4
A let x = 10;
B while (x > 0) [
C x = x - 1;
Question 5
A let y = 1;
B while (y <= 5) {
C y = y + 1;
D ]

Answer Key

While Loops
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
A — Yes
Q4
A — Yes
Q5
B — Yes
Section B: Match the Pairs
Q1
while (count <= 3) { ... } → Repeats the block while count is 3 or less
Q2
while (isRunning) { ... } → Repeats the block while isRunning stays true
Q3
Updates a variable so the loop eventually stops → count = count + 1;
Q4
Runs forever because the condition never becomes false → Infinite loop
Q5
while n > 0 { n = n - 1; } → Wrong - the condition needs parentheses
Q6
while (z = 3) { z = z + 1; } → Wrong - use a comparison operator, not = for assignment
Section C: Definitions
Q1
while — Repeats a block of code as long as its condition inside parentheses stays true.
Section D: Fill in the Blanks
Q1
while (count <= 3) {
Q2
while (count <= 3) {
Q3
while (n > 0) { n = n - 1; }
Q4
while (isRunning) {
Q5
while (i < 5) { i = i + 1; }
Section E: Spot the Error
Q1
B — 'while' is misspelled as 'Wile'.
Q2
A — The condition needs parentheses: while (n > 0).
Q3
C — This line is missing a semicolon, but that's not required in JS.
Q4
B — Code blocks use curly braces {}, not square brackets [].
Q5
D — The block must close with }, not ].