While Loops, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
While Loops in one minute
What students need to know
This line moves count closer to making the condition false. This happens when the loop variable is never updated properly.
The loop keeps running until the condition becomes false. JavaScript is case-sensitive, so 'while' must be lowercase.
Mistakes students make
- 'while' is misspelled as 'Wile'.
- The condition needs parentheses: while (n > 0).
- This line is missing a semicolon, but that's not required in JS.
- Code blocks use curly braces {}, not square brackets [].
What students will learn in this module
Every question in Module 19
Quick questions
- Does a while loop repeat as long as its condition is true?AYesBNo
- Must 'while' be written in lowercase?ANoBYes
- Does a while loop's condition need to be in parentheses?AYesBNo
- Can forgetting to update a variable cause an infinite loop?AYesBNo
- Does a while loop's code block use curly braces?ANoBYes
Key terms
Students type the meaning of:
while
Match the pairs
while (count <= 3) { ... }Match it to what it doeswhile (isRunning) { ... }Match it to what it doesUpdates a variable so the loop eventually stopsMatch it to what it doesRuns forever because the condition never becomes falseMatch it to what it doeswhile n > 0 { n = n - 1; }Match it to what it doeswhile (z = 3) { z = z + 1; }Match it to what it doesFill in the blanks
_____ (count <= 3) {Hint: Type the keyword that starts a while loop.
while (count __ 3) {Hint: Type the operator that checks less than or equal to.
while (n > 0) { n = n _ 1; }Hint: Type the operator that subtracts one from n.
_____ (isRunning) {Hint: Type the keyword that starts a while loop.
while (i < 5) { i = i _ 1; }Hint: Type the operator that adds one to i.
Spot the error
Question 1
Alet count = 1;BWile (count <= 3) {Ccount = count + 1;
Question 2
Awhile n > 0 {Blet n = 5;Cn = n - 1;
Question 3
Alet i = 0;Bwhile (i < 5) {Cconsole.log(i)
Question 4
Alet x = 10;Bwhile (x > 0) [Cx = x - 1;
Question 5
Alet y = 1;Bwhile (y <= 5) {Cy = y + 1;D]
