dowhile Loop in JavaScript | Control Statements in JS

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 –

do
{
    statements..
}
while (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 – doWhile loop in javascript flowchart diagram

Program example –

<script type = "text/javaScript"> 
// JavaScript program to illustrate do-while loop 
  
    var x = 21; 
  
    do 
    { 
        // The line while be printer even 
        // if the condition is false 
        document.write("Value of x:" + x + "<br />"); 
  
        x++; 
    } while (x < 20); 
  
< /script>

Output –

Value of x: 21
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *