while
StatementA while
statement repeatedly executes a section of code so long as a given condition is true. We can use a while
to write a program to sum the numbers from 1 through 10 inclusive as follows:
#include <iostream>
int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return 0;
}
When we compile and execute this program, it prints
Sum of 1 to 10 inclusive is 55
As before, we start by including the iostream
header and defining main
. Inside main
we define two int
variables: sum
, which will hold our summation, and val
, which will represent each of the values from 1 through 10. We give sum
an initial value of 0
and start val
off with the value 1
.
The new part of this program is the while
statement. A while
has the form
while (condition)
statement
A while
executes by (alternately) testing the condition and executing the associated statement until the condition is false. A condition is an expression that yields a result that is either true or false. So long as condition is true, statement is executed. After executing statement, condition is tested again. If condition is again true, then statement is again executed. The while
continues, alternately testing the condition and executing statement until the condition is false.
In this program, the while
statement is
// keep executing the while as long as val is less than or equal to 10
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
The condition uses the less-than-or-equal operator (the <=
operator) to compare the current value of val
and 10
. As long as val
is less than or equal to 10, the condition is true. If the condition is true, we execute the body of the while
. In this case, that body is a block with two statements:
{
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
A block is a sequence of zero or more statements enclosed by curly braces. A block is a statement and may be used wherever a statement is required. The first statement in this block uses the compound assignment operator (the +=
operator). This operator adds its right-hand operand to its left-hand operand and stores the result in the left-hand operand. It has essentially the same effect as writing an addition and an assignment:
sum = sum + val; // assign sum + val to sum
Thus, the first statement in the block adds the value of val
to the current value of sum
and stores the result back into sum
.
The next statement
++val; // add 1 to val
uses the prefix increment operator (the ++
operator). The increment operator adds 1
to its operand. Writing ++val
is the same as writing val = val + 1
.
After executing the while
body, the loop evaluates the condition again. If the (now incremented) value of val
is still less than or equal to 10, then the body of the while
is executed again. The loop continues, testing the condition and executing the body, until val
is no longer less than or equal to 10.
Once val
is greater than 10, the program falls out of the while
loop and continues execution with the statement following the while
. In this case, that statement prints our output, followed by the return
, which completes our main
program.
Exercise 1.9: Write a program that uses a while
to sum the numbers from 50 to 100.
Exercise 1.10: In addition to the ++
operator that adds 1
to its operand, there is a decrement operator (--
) that subtracts 1
. Use the decrement operator to write a while
that prints the numbers from ten down to zero.
Exercise 1.11: Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.