The while loop is used to run a statement or a series of statements as long as a condition remains true. The loop tests the condition before the statement is executed. If the condition is true, the statement is executed. But if the condition is false, the statement is skipped and the statements following the end of the while loop is executed.
In this loop the variable needs to be initialized before the start of the loop, and the variable increments each time the statement is executed. When the value of the variable reaches a specified value, the condition returns false and the loop ends.
The syntax of the while loop is as follows:
statements();
}
The loop performs the following steps:
- Evaluate the condition.
- If condition returns true, go to step 3. Otherwise, execute the statements following the end of the while loop.
- Execute the loop statement.
- Go back to step 1.
In he following example, the while loop is executed till the value of the variable x is less than 10.
x=1
while(x<10){
trace(”X is equal to ” +x); // output: 1,2,3,4,5,6,7,8,9
x++ // same as x = x + 1
}
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.
