This is more or less a copy of the FAQ in the Archive just to keep it easier to find it and reanble the response functionality:
https://www.codecademy.com/forum_questions/5478568f282ae322d6006028
As this section doesn’t have a FAQ so far lets make some
in this thread. Feel encouraged to extend it, but please post questions
and in particular questions about your code as a seperate question in
another thread to keep this readable.
Now lets start with the explanation of loops in general.
I’d say: “The idea of a loop is to group reoccurring code that is pretty similar and executed in a row in one structure.”
The while loop:
Syntax:
while(condition){
statements;
}
How to use: A while loop is pretty simple it runs the code in the {} while the condition is true. So its basically a loop of:
check condition → run code / exit → check condition → run code / exit → …
When to use: As the while loop is just a reoccuring
if condition it does not need to know how often it is executed or still
has to be executed so if you don’t know it either use the while loop.
Example
console.log("get the number of times a coin is not heads");
// get randomly either 0 or 1
var coin = Math.floor(Math.random()*2);
var counter = 1; //count "coin flips"
while(coin != 1){
coin = Math.floor(Math.random()*2);
counter++;
}
console.log(counter);
console.log("Force the user to input correct password");
var pw = prompt("Please enter your password");
while(pw != 12345){
pw = prompt("Please enter your password");
}
The do-while loop:
Syntax:
do{
statements;
}while(condition);
How to use:
Write the code you want to loop over in
the {} between do and the while(condition) where I wrote statements,
this code is executed once without any condition and then its the same
loop as for the while loop:
execute statements > check condition → jump back to do / exit
→ check condition → …
When to use: The do-while loop is some kind of
inverted while loop where the {} is placed befor the condition. It’s
main purpose is to avoid repeating yourself. See the examples for more.
Examples: Pretty much the same as for while but
shortend, if you do not need the statements inside of the loop to be
executed once unconditioned take the while loop otherwise this is a good
alternativ:
Example
console.log("get the number of times a coin is not heads");
// get randomly either 0 or 1
var counter = 0; //count "coin flips"
do{
var coin = Math.floor(Math.random()*2);
counter++;
}while(coin != 1);
console.log(counter);
console.log("Force the user to input correct password");
do{
var pw = prompt("Please enter your password");
}while(pw != 12345);
The for-loop:
Syntax:
for(initalize;condition;manipulate){
statements;
}
How to use:
initialize: this part is executed only once before
anything else in the loop and is regularly used to initialize the
counting variable e.g. var i=0;
condition:this part is executed before the code in the {} is executed or skipped. Normally its the condition when to stop e.g. i < stoppingValue
manipulate:this part is executed after the execution of
the stuff in the {} and normally its used to change the value of the
counting variable to get it nearer to the stopping value e.g. i++
The loop is:
initialize → check condition → run code /exit →
manipulate → check condition → run code /exit →
manipulate → …
When to use: For loops in general is used when you
know the number of loops you want to do. In this case you can imagine a
starting value, a stopping value and a stepwidth which this loop manages
all nicely in one line.
Example
console.log("Count from 0 to 10 with a stepwidth of 1:");
for(var i=0;i<10;i++){
console.log(i);
}
console.log("Count from 12 to 36 with a stepwidth of 2:");
for(var i=12;i<37;i+=2){
console.log(i);
}
console.log("Countdown from 10");
for(var i=10;i>0;i--){
console.log(i);
}