do while statement in javascript

do while statement in javascript (do while loop)

In do while, the statements are executed first and then the condition is checked.

Syntax

Flowchart

do..while statement flowchart

Example

Try Yourself

Execution flow of above example

Step 1 - image explanation
  • Variable a is created and value 1 is assigned to it (var a=1).
  • Alert statement in do..while loop executes (alert(1)).
  • 1 is incremented in variable a (a=a+1)
  • A condition (a<3) is checked, and it is true (2<3).
  • Control moves to the first statement of the do..while block.
Step 2 - image explanation
  • Alert statement in do..while loop executes (alert(2)).
  • 1 is incremented in variable a (a=a+1)
  • A condition (a<3) is checked, and it is false (3<3).
  • Control moves to the next statement (alert("Finished")).