The while Statement
Now we can move onto more advanced statements such as the while statement, the do...while statement, and the for statement.
The while statement has the syntax:
while (condition) statement;
First the condition is evaluated. If it is nonzero(true), the statement is executed and the condition is evaluated again. These two steps are repeated until the condition evaluates to zero (false).
Here is an example of how the while statement works:
The above program first asks the user to enter a number, then it checks the condition (n > 0). If you entered the number 3, the condition would evaluate as true, and execute the code in the block. The program will continue to ask the user for another number to cube, if you want it to stop then you would have to type the number 0. If you did type in a 0, the condition would evaluate false and the loop would end. You could also write this same code using a do while statement. Please Rate this Code: