The while Statement
#include <iostream>
int main() {
std::cout << 1 << '\n';
std::cout << 2 << '\n';
std::cout << 3 << '\n';
std::cout << 4 << '\n';
std::cout << 5 << '\n';
}
1
2
3
4
5
How would you write the code to count to 10,000? Would you copy, paste, and modify 10,000 printing statements? You could, but that would be impractical! Counting is such a common activity, and computers routinely count up to very large values, so there must be a better way. What we really would like to do is print the value of a variable (call it count), then increment the variable (count++), and repeat this process until the variable is large enough (count == 5 or perhaps count == 10000).
#include <iostream>
int main()
{
int count = 1; // Initialize counter
while (count <= 5)
{
std::cout << count << '\n'; // Display counter, then
count++; // Increment counter
}
}
The while statement has the general form:

- The reserved word while begins the while statement.
- The Boolean expression condition determines whether the body will be (or will continue to be) executed. The expression must be enclosed within parentheses as shown.
- The statement is the statement to be executed while the Boolean expression is true. The statement makes up the body of the while statement.
#include <iostream>
int main() {
char input; // The users choice
int count = 0; // The current count
bool done = false; // We are not done
while (!done) {
// Print the current value of count
std::cout << count << '\n';
std::cout << "Please enter \"Y\" to continue or \"N\" to quit: ";
std::cin >> input;
// Check for "bad" input
if (input != 'Y' && input != 'y' && input != 'N' && input != 'n')
std::cout << "\"" << input << "\"" << " is not a valid choice" << '\n';
else if (input == 'Y' || input == 'y')
count++; // Keep counting
else if (input == 'N' || input == 'n')
done = true; // Quit the loop
}
}
0
Please enter "Y" to continue or "N" to quit: y
1
Please enter "Y" to continue or "N" to quit: y
2
Please enter "Y" to continue or "N" to quit: y
3
Please enter "Y" to continue or "N" to quit: q
"q" is not a valid choice
3
Please enter "Y" to continue or "N" to quit: r
"r" is not a valid choice
3
Please enter "Y" to continue or "N" to quit: W
"W" is not a valid choice
3
Please enter "Y" to continue or "N" to quit: Y
4
Please enter "Y" to continue or "N" to quit: y
5
Please enter "Y" to continue or "N" to quit: n
The initialization of input to zero coupled with the condition input >= 0 of the while guarantees that program will execute the body of the while loop at least once. The if statement ensures that a negative entry will not be added to sum
#include <iostream>
int main() {
int input, sum = 0;
std::cout << "Enter numbers to sum, type 'q' to end the list:";
while (std::cin >> input)
sum += input;
std::cout << "Sum = " << sum << '\n';
}
#include <iostream>
int main() {
int power = 1;
while (power <= 1000000000) {
std::cout << power << '\n';
power *= 10;
}
}
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
#include <iostream>
#include <iomanip>
// Print the powers of 10 from 1 to 1,000,000,000
int main() {
int power = 1;
while (power <= 1000000000) {
// Right justify each number in a field 10 wide
std::cout << std::setw(10) << power << '\n';
power *= 10;
}
}
Nested Loops
Just like in if statements, while bodies can contain arbitrary C++ statements, including other while statements. A loop can therefore be nested within another loop. To see how nested loops work, consider a program that prints out a multiplication table. Elementary school students use multiplication tables, or times tables, as they learn the products of integers up to 10 or even 12. Figure 6.3 shows a 10 × 10 multiplication table. We want our multiplication table prog
#include <iostream>
int main() {
int size; // The number of rows and columns in the table
std::cout << "Please enter the table size: ";
std::cin >> size;
// Print a size x size multiplication table
int row = 1;
while (row <= size) { // Table has 10 rows.
std::cout << "Row #" << row << '\n';
row++; // Next row
}
}