Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The while statement lets you repeat a statement until a specified expression becomes false.
Syntax
iteration-statement:
while ( expression ) statement
The expression must have arithmetic or pointer type. Execution proceeds as follows:
The
expressionis evaluated.If
expressionis initially false, the body of thewhilestatement is never executed, and control passes from thewhilestatement to the next statement in the program.If
expressionis true (nonzero), the body of the statement is executed and the process is repeated beginning at step 1.
The while statement can also terminate when a break, goto, or return within the statement body is executed. Use the continue statement to terminate an iteration without exiting the while loop. The continue statement passes control to the next iteration of the while statement.
Here's an example of the while statement:
while ( i >= 0 )
{
string1[i] = string2[i];
i--;
}
This example copies characters from string2 to string1. If i is greater than or equal to 0, then string2[i] is assigned to string1[i] and i is decremented. When i reaches or falls below 0, execution of the while statement terminates.