While Loop in Javascript - Control Statements in Js
While Loop in Javascript - Control Statements in Js
The while loop is primarily used when the number of iterations in not known in advanced and the iterations are based on
some boolean condition.
Syntax –
1Value of x:1
2Value of x:2
3Value of x:3
4Value of x:4
JavaScript Programming Programming Languages Web Development
The do/while statement is used when you want to run a loop at least one time, no matter what.
Syntax –
1do
2{
3 statements..
4}
5while (condition);
1. do while loop starts with the execution of the statement(s). There is no checking of any condition for the
first time.
2. After the execution of the statements, and update of the variable value, the condition is checked for true or
false value. If it is evaluated to true, next iteration of loop starts.
3. When the condition becomes false, the loop terminates which marks the end of its life cycle.
4. It is important to note that the do-while loop will execute its statements atleast once before any condition is
checked, and therefore is an example of exit control loop.
Flowchart –
Program example –
1Value of x: 21