Loops
Computers are good at doing things over and over again.
A LOOP is when a program does something repeatedly, until some CONDITION is met.
While Loop
The simplest kind of loop is the while
loop.
The loop below counts from one to five, followed by Ha ha ha!
each time.
let count = 1;
while (count <= 5) {
// the code below runs once per loop cycle
console.log('' + count + ', Ha ha ha!');
count = count + 1;
}
In English this reads, "While some condition is true, do some action".
While True
To loop forever, set the CONDITION to true
:
while (true) {
// will loop forever
// because true is always true
console.log('Hello');
}
To stop the loop, hold down the CONTROL+C keys, CRTL+C.
One Infinite Loop
Fun Fact: The address of Apple Headquarters is 1 Infinite Loop
Wikimedia Commons, image licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Attribution: Joe Ravi
Counting Forever
Write the following code in a file called, count.js
.
Then run the program using node count.js
.
let count = 1;
while (true) {
console.log(count);
count = count + 1;
}
Remember: CRTL+C will stop the program
LAB: Count to One Hundred
Update count.js
so that it counts from 1 to 100, inclusive.
let count = 1;
while (/* count is less than or equal to 100 */) {
console.log(count);
count = count + 1;
}
Solution: Count to 100
Hint
Remember, a while loop only runs while the check evaluates true
while(/*this expression is true...*/) {
// execute this code
}
Solution
let count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
Did your program stop at 100, 101, or 99?
While Loops Breakdown
- A
while
statement checks the condition every cycle-
IF condition evaluates to
true
, THEN then loop again -
IF condition evaluates to
false
, THEN stop looping and proceed to the code after the loop
-
IF condition evaluates to
let count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
While Breakdown
- Create a variable named
count
and set the value to1
- Begin loop if
count
is less than100
-
1
is less than100
, so run the body of the loop - Repeat, starting from the condition at top of the loop
let count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
While Breakdown Cont
- Step 1
let count = 1
- Step 2
while (count <= 100) { ... }
- Step 3
console.log(count);
count = count + 1;
- Step 4
while (count <= 100) { ... }
Break in a While Loop
If the program encounters the keyword break
the loops stops.
Alternative code counting from 1 to 100 using break
is below
let count = 0;
while (true) {
if (count > 100) {
break;
}
console.log(count);
count = count + 1;
}
For Loops
A while
loop is simple, but requires a variable in the condition or the break
keyword.
There is another kind of loop called a for
loop.
for (let count = 1; count <= 100; count++) {
console.log(count);
}
For Loop Breakdown
The for (...)
part of a for
loop is where the loop:
- Initializes a variable to track
- Checks whether to run again
- Updates the variable after the loop body runs.
for (let count = 1; count <= 100; count++) {...}
For Loop Breakdown Cont
Think about the three parts as three separate lines of code.
let count = 0;
<- Initialize a variable to trackcount <= 100;
<- Determine whether to run againline++;
<- Update the variable after the loop body runs
For Loop Body
The body
of the loop is the code that runs on each time through the loop
for (let count = 0; count <= 100; count++) {
// this code will run each time we loop
// since `count` changes each time,
// a different number printed each cycle
console.log(count);
}
Everything inside the curly braces is the body
LAB: One Potato
Apply what you learned about while
and for
loops to print a poem.
Create a file called potato.js
that prints the following when executed by node
.
1 potato,
2 potato,
3 potato,
4!
5 potato,
6 potato,
7 potato,
More!
Make sure to use a
while
orfor
loop in your solution
Counting Potatoes Solution (pt.1)
Separate the solution into two parts.
Create a function poemLine
does only one thing; create the correct output for a given line number.
Solution
function poemLine(lineNumber) {
if (lineNumber === 8) {
return "More!"
} else if (lineNumber === 4) {
return "4!"
} else {
return lineNumber + " potato,"
}
}
Counting Potatoes Solution (pt.2)
The second part of the solution only loops, based on a counter lineNumber
.
Solution
function poemLine(lineNumber) {
if (lineNumber === 8) {
return "More!"
} else if (lineNumber === 4) {
return "4!"
} else {
return lineNumber + " potato,"
}
}
// New Code Below
let lineNumber = 1;
while (lineNumber <= 8) {
console.log(poemLine(lineNumber));
lineNumber = lineNumber + 1;
}
Counting Potatoes, alternative with break
Another solution using the break
to stop looping after eight cycles.
Solution
function poemLine(lineNumber) {
if (lineNumber === 8) {
return "More!"
} else if (lineNumber === 4) {
return "4!"
} else {
return lineNumber + " potato,"
}
}
// New Code Below
let lineNumber = 1;
while (true) {
console.log(poemLine(lineNumber));
lineNumber = lineNumber + 1;
if (lineNumber > 8) {
break;
}
}
Suggested Projects
- Loops
- While Loop
- While True
- One Infinite Loop
- Counting Forever
- LAB: Count to One Hundred
- Solution: Count to 100
- While Loops Breakdown
- While Breakdown
- While Breakdown Cont
- Break in a While Loop
- For Loops
- For Loop Breakdown
- For Loop Breakdown Cont
- For Loop Body
- LAB: One Potato
- Counting Potatoes Solution (pt.1)
- Counting Potatoes Solution (pt.2)
- Counting Potatoes, alternative with `break`
- Labs
- Projects