while
loop runs while a condition is true (infinitely), for
loop runs a certain number of times. To stop while
loops you can use a break
statement. (Or you need a statement that at some time evaluates to false so the loop could end). To keep track of how many times a loop has run, you start with a counter variable at 0 and increment it every time the loop runs.
while loops run when a condition is true
. It’s an infinte loop, meaning it keeps runing while the statement is true. to stop it form running, you need to use a break
statement;
Example: exit loop when user enters quit
1var search;
2while (true) {
3 search = prompt('Do you wanna quit?');
4 if(search === 'quit') {
5 break;
6 }
7}
The do
part will run as long as the while
statement is true.
Do while loop is the same as while loop. The only difference is that it runs at least once, before getting to the point where it evaluates the while
part.
1do {
2 // something
3} while ()
You can use do while loops where you want to run the same code until something is obtained, like a valid email address, or an answer
FYI: A flag
is a variable that holds a true or false value.