while
statement. A
while
statement makes a program repeat a
statement or group of statements (block). Such repetitions are called loops. This
web page will help you to master different types of
while
loops. Contents:
while
Loop Syntax
while ( Boolean expression )
{
//This will repeat while the Boolean expression is true
}
A while
statement allows a program to continue executing a statement as long
as the value of the Boolean expression is while
. The Boolean expression is
first tested when execution first reaches the while
.
If it is true
, the following statement or block will run
once, and the Boolean expression will be tested again. This continues until
the Boolean expression evaluates to false
.
When it is false
, execution of the program
continues with the statement immediately following the while statement.
while
Loop
sum = 0;
count = 1;
while (count <= 10)
{
cout << "Enter a value: ";
cin >> value;
sum = sum + value;
count++;
}
cout << "The sum of the 10 numbers is "
<< sum << endl;
The variables sum
and
count
are assigned the values 0 and 1
respectively. (count <= 10)
is
evaluated.count
is less than or
equal to 10, the expression is true
and
the compound statement (block) associated with the
while
statement is executed.sum
. count
is incremented by 1.
When the block of statements ends, the
while
expression is evaluated again.
Because the value stored in the count
is
still less than 10, the block associated with the
while
statement is executed again. This
process countinues until count
contains
the value 11. At that time, the expression is no longer
true
, the body of the
while
loop is not executed again, and
the execution continues with the statement immediately following the
while
statement. In this case, it
continues with a cout
that sends the
labeled answer to the output stream.
cin
until a negative value is encountered.
sum = 0;
cout << "Enter a value: ";
cin >> value;
while (value >= 0)
{
sum = sum + value;
cout << "Enter a value: ";
cin >> value;
}
cout << "The sum of the values prior to a negative value is "
<< sum << endl;
For this same problem, we can write a program in another way:
sum = 0;
cout << "Enter a value: ";
cin >> value;
// Set the boolean variable moreData to true if the first data item is
// not negative; false otherwise.
if (value >= 0)
moreData = true;
else
moreData = false;
while (moreData)
{
sum = sum + value;
cout << "Enter a value: ";
cin >> value;
// Reset the value of moreData
if (value >= 0)
moreData = true;
else
moreData = false;
}
cout << "The sum of the values prior to a negative value is "
<< sum << endl;
sum
is set to zero and the first data item
value
is read outside of the loop.value
is compared to zero and its result
is stored in the Boolean variable
moreData
.moreData
has a value of false, and
the body of the loop is not executed.moreData
has a value of true, and
the body of the loop is entered and executed. value
is added to the
sum
and the next data item is read. moreData
is reset. The expression is
tested again.moreData
becomes
false
. When this happens, the body of the
while
loop is not executed again. The sum
of the non-negative numbers is sent to the output stream.
The above while loop is also called a flag-controlled loop. The
variable moreData
is the flag. It has
the equivalent effect as the following:
sum = 0;
cout << "Enter a value: ";
cin >> value;
// Set moreData to true if the first data item is not negative;
// false otherwise.
moreData = value >= 0;
while (moreData)
{
sum = sum + value;
cout << "Enter a value: ";
cin >> value;
// Reset the value of moreData
moreData = value >= 0;
}
cout << "The sum of the values prior to a negative value is "
<< sum << endl;
while
statement
is called a loop or looping statement. The statement that is being executed
within the loop is called the body of the loop.
There are three basic steps that must be done for a loop to operate properly.
©Department of Computer Science, University of Regina.