What is the while loop?
What is the while loop?
Rating:
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
}
Rating:
Other articles
- Tip for combining the regular lasso tool with the polygon lasso tool.
- Tip for removing all hints from an object at the same time
- What is the use of the Color Mixer panel?
- How to create frame-by-frame animation?
- Tip for changing the docked toolbar to a floating toolbar.