The do while loop is used to execute the statement or a series of statements inside the loop as long as a condition remains true. The loop executes the statements before testing the condition. It ensures that the statements are executed at least once.
In the do while loop, the variable needs to be initialized and increments each time the statements are executed. When the value of variables reaches a specified value, the condition returns false and the loop ends.
The syntax of the do while loop is follows:
statement(s)
} while (condition)
The loop performs the following 4 steps:
- Executes the statements inside the loop.
- Evaluate the condition.
- If the condition returns true, go to step 4. Otherwise, execute the next line following the end of the do while loop.
- Go back to step 1.
In the following example, the do while loop is executed till the value of the variable x is less than 10.
do{
trace(”X is equal to ” +x); // output: 1,2,3,4,5,6,7,8,9
x++ // same as x = x + 1
}while(x<10)
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
