JavaScript Programming Programming Languages Web Development
while Loop in JavaScript | Control Statements in
JS
October 22, 2018 Tanmay Sakpal 0 Comments javascript while loop, while loop, while loop in javascript
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The
while loop can be thought of as a repeating if statement. Once the expression becomes false, the loop terminates.
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 –
1while (boolean condition)
2{
3 loop statements...
4}
While loop starts with the checking of condition. If it evaluated to true, then the loop body statements are
executed otherwise first statement following the loop is executed. For this reason it is also called Entry control
loop
Once the condition is evaluated to true, the statements in the loop body are executed. Normally the
statements contain an update value for the variable being processed for the next iteration.
When the condition becomes false, the loop terminates which marks the end of its life cycle
Flowchart –
Program example –
1 <script type = "text/javaScript">
2 // JavaScript program to illustrate while loop
3
4 var x = 1;
5
6 // Exit when x becomes greater than 4
7 while (x <= 4)
8 {
9 document.write("Value of x:" + x + "<br />");
10
11 // increment the value of x for
12 // next iteration
13 x++;
14 }
15
16< /script>
Output –
1Value of x:1
2Value of x:2
3Value of x:3
4Value of x:4
JavaScript Programming Programming Languages Web Development
dowhile Loop in JavaScript | Control
Statements in JS
October 22, 2018 Tanmay Sakpal 0 Comments dowhile loop, dowhile loop in javascript, javascript dowhile loop
do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and
therefore is an example of Exit Control Loop. The do/while statement creates a loop that executes a block of code once,
before checking if the condition is true, then it will repeat the loop as long as the condition is true.
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 –
1 <script type = "text/javaScript">
2 // JavaScript program to illustrate do-while loop
3
4 var x = 21;
5
6 do
7 {
8 // The line while be printer even
9 // if the condition is false
10 document.write("Value of x:" + x + "<br />");
11
12 x++;
13 } while (x < 20);
14
15< /script>
Output –
1Value of x: 21