The syntax of a basic while-loop is straightforward, requiring only the while keyword and a set of parentheses surrounding a Boolean-valued test expression. The program evaluates the test expression before execution enters the loop body. Evaluating the test before entering the body makes while-loops test at the top loops. They are indefinite loops in the sense that the code may not explicitly denote the number of iterations that will take place, as suggested by the following pseudo-code:
while (there are still lines in the file)
get a line and process it;
While loop continues running or iterating until the test expression evaluates to false (i.e., to a zero-equivalent value). The following figure illustrates the basic pattern of the while-loop and its logical behavior.
while (test)
statement;
while (test)
{
statement-1;
. . .
statement-n;
}
(a)
(b)
(c)
Basic while-loop behavior.
The "test" expression executes once at the beginning of each iteration. The loop continues while the test evaluates to true, explaining why the language uses the while keyword. Programmers write "test" expressions based on some condition or variable in the program that changes while the loop runs. For example, reading the last line in a data file, processing the last element in a list, etc. While-loops are a test at the top loops, with the test occurring before control enters the loop's body. The program skips the loop body if the test is initially false.
A single-statement while-loop
A while-loop with a compound or block statement
A logic diagram of the while-loop
Infinite while-Loops
while (true)
{
statements;
if (some condition)
break;
}
while (1)
{
statements;
if (some condition)
break;
}
bool loop = true;
while (loop)
{
statements;
if (some condition)
loop = false;
}
(a)
(b)
(c)
Infinite while-loops. Programmers can create infinite loops with either the for or while keywords - which one you choose is a matter of personal taste.
Infinite while-loops are implemented with a (usually Boolean) constant.
An older C-style infinite loop. Lacking the bool keyword, C uses any non-zero value in its place.
Technically, this example isn't an infinite loop, but it does require a nested if-statement to force the loop to end. Novice programmers often write the loop as while (loop == true), but the == operator is not required when the test uses a Boolean variable.
Some rare programs (e.g., daemons) loop indefinitely, never ending, and never "breaking out" of the loop.
For- vs. While-Loops
for (init; test; update)
statements;
init;
while (test)
{
statements;
update;
}
For-loop and while-loop equivalence. Although their organizations differ, for-loops and while-loops typically have the same interchangeable elements. Which loop you choose is often a matter of personal taste. Nevertheless, programmers generally use for-loops when the beginning and ending conditions are known or calculated before the program begins the loop. Alternatively, they use while-loops when the ending conditions are dynamic and unknown when the program begins the loop.
Examples
int counter = 0;
while (counter < 10)
{
cout << counter << " ";
counter++;
}
int counter = 0;
while (counter > 10)
{
cout << counter << " ";
counter++;
}
0 1 2 3 4 5 6 7 8 9
Produces no output
Simple while-loop examples. These examples violate the advice just given, and we can more properly implement them with for-loops, but the intent is to keep the while-loop syntax and behavior simple. Subsequent examples demonstrate more typical while-loops. While-loops are a test at the top loop, the loop body never runs if the test is initially false.