Computers are good at doing things over and over again.
A LOOP is when a program does something repeatedly, until some CONDITION is met.
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".
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.
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
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
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;
}
while(/*this expression is true...*/) {
// execute this code
}
let count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
Did your program stop at 100, 101, or 99?
while
statement checks the condition every cycle
true
, THEN then loop againfalse
, THEN stop looping and proceed to the code after the looplet count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
count
and set the value to 1
count
is less than 100
1
is less than 100
, so run the body of the looplet count = 1;
while (count <= 100) {
console.log(count);
count = count + 1;
}
let count = 1
while (count <= 100) { ... }
console.log(count);
count = count + 1;
while (count <= 100) { ... }
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;
}
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);
}
The for (...)
part of a for
loop is where the loop:
for (let count = 1; count <= 100; count++) {...}
Think about the three parts as three separate lines of code.
let count = 0;
<- Initialize a variable to track
count <= 100;
<- Determine whether to run again
line++;
<- Update the variable after the loop body runs
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
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
Separate the solution into two parts.
Create a function poemLine
does only one thing; create the correct output for a given line number.
function poemLine(lineNumber) {
if (lineNumber === 8) {
return "More!"
} else if (lineNumber === 4) {
return "4!"
} else {
return lineNumber + " potato,"
}
}
The second part of the solution only loops, based on a counter lineNumber
.
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;
}
break
Another solution using the break
to stop looping after eight cycles.
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;
}
}
/