The while statement repeatedly executes a statement or group of statements when a given condition is true. The condition is tested before the loop body is executed.
The syntax format is as follows:
while(condition){ statement(s);}
Here, statement(s) can be a single statement or a block of code composed of several statements.
condition can be any expression, and the loop is executed when the condition is true. When the condition is false, the program flow exits the loop.
In the diagram, the key point about the while loop is that the loop may not execute even once. When the condition is false, the loop body is skipped and the next statement immediately following the while loop is executed directly.
In the program, the loop body is executed when the variable $a is less than 20, and exits the loop when the variable $a is greater than or equal to 20.
Executing the above program, the output result is:
The value of a is: The value of 10a is: The value of 11a is: The value of 12a is: The value of 13a is: The value of 14a is: The value of 15a is: The value of 16a is: The value of 17a is: The value of 18a is: 19