Loops allow one or more lines of code within your program to execute multiple times. Below are a few examples of how a loop could be used in a program.
This chapter will examine the while loop and the next chapter looks at the for loop.
The while statement is followed by a condition (like the if statement). The code that will be executed multiple times (looped) is contained within braces. This code will execute as long as the condition evaluates to true. The following code demonstrates a simple while loop that prints the word "Hello" five times: HelloHelloHelloHelloHello.
int A = 1;
while (A <= 5)
{
cout << "Hello";
A++;
}
In the previous code, we declared a variable A and assigned it a value of 1. The condition tells the while loop to execute the code within the braces as long as A is less than or equal to 5. Within the braces, the cout statement prints "Hello" and the A++ statement increments A. The next complete program shows another while loop. On the right side is the same program without using a loop.
Program 12.1 – While Loop |
|
#include <iostream>
main() |
// The same program without using a loop
#include <iostream>
main() |
Output |
In the previous program, we declared a variable B and assigned a value of 1. The condition on the while loop says to execute the code within braces as long as B is less than 5.
Be careful when programming with loops that you do not create a program with an infinite loop. An infinite loop is one in which the condition will always be equal to true. The following program demonstrates an infinite loop.
Program 12.2 – Infinite Loop |
// If you run this, you will have to manually stop the program
#include <iostream>
main() |
Output
5678910111213141516171819202122232425262728293031323334353637383940 |
In the preceding program, we declared a variable Z and made it equal to 5. The condition in the while loop says to execute as long as Z is greater than one. Since we increment Z during each iteration (Z++), Z will always be greater than 1. If you ran this program, then you may be wondering how to stop it – this is covered in the sequel C++ for Geniuses++. Just kidding!! Terminating a program that is in an infinite loop will depend on the compiler you are using. You can try pressing Ctrl-C. If this does not work, then you may need to close the window that is running the program. If this does not work, then it may be time to buy a new computer.
A while loop can be used for writing a program that contains a menu of options for the user. The loop will allow you to create a program that will keep running until the user selects the option to exit. In the next program, the while loop will keep looping until the user enters 5 to exit.
Program 12.3 – While Loop for Menus |
#include <iostream> |
Output
1 = See the highest mountain
K2
Enter your choice: 5 |
What will each of the following while loops output?
1. int A
= 1;
while (A < 5)
{
cout << A;
A++;
}
2. int B
= 5;
while (B >= 0)
{
cout << B;
B--;
}
3. int A
= 1, B = 6;
while (A <= 7)
{
cout << A + B << " ";
A++;
B--;
}
4. int X
= 2;
while (X <= 128)
{
cout << X << " ";
X = X * 2;
}
5. int X
= 1;
char Y = '*';
while (X <= 12)
{
cout << Y;
X = X + 2;
}
6. int J
= 10;
while (J >= 0)
{
if ((J % 2) == 0)
cout << "yes ";
else
cout << "no ";
J--;
}