Chapter 12 While Loop

12.1  Basic While Loop 

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>
using namespace std;

main()
{
  int B = 1;
  while (B < 5)
  {
    cout << "Hello\n";
    B++;
  }
  cout << endl;
}

// The same program without using a loop

#include <iostream>
using namespace std;

main()
{
  cout << "Hello\n";
  cout << "Hello\n";
  cout << "Hello\n";
  cout << "Hello\n";
  cout << endl;
}

Output
Hello
Hello
Hello
Hello

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.

12.2  Infinite Loops 

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>
using namespace std;

main()
{
  int Z = 5;
  while (Z > 1)
  {
    cout << Z;
    Z++;
  }
}

Output

5678910111213141516171819202122232425262728293031323334353637383940
4142434445464748495051525354555657585960616263646566676869707172737
475767778798081828384858687888990919293949596979899100101102103...

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.

12.3  Using While Loops for Menus 

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>
using namespace std;

main()
{
  short int Choice = 0;
  while (Choice != 5)
  {
    cout << "1 = See the highest mountain\n";
    cout << "2 = See the second highest mountain\n";
    cout << "3 = See the third highest mountain\n";
    cout << "4 = See the fourth highest mountain\n";
    cout << "5 = Exit\n";
    cout << "Enter your choice: ";
    cin >> Choice;
    if (Choice == 1)
      cout << "Everest\n";
    if (Choice == 2)
      cout << "K2\n";
    if (Choice == 3)
      cout << "Kangchenjunga\n";
    if (Choice == 4)
      cout << "Lhotse\n";
    cout << "-----------------------------------\n";
  }
  cout << "Hope you enjoyed this mountain trivia!\n";
}

Output

1 = See the highest mountain
2 = See the second highest mountain
3 = See the third highest mountain
4 = See the fourth highest mountain
5 = Exit
Enter your choice: 2

K2
-----------------------------------
1 = See the highest mountain
2 = See the second highest mountain
3 = See the third highest mountain
4 = See the fourth highest mountain
5 = Exit

Enter your choice: 5
-----------------------------------
Hope you enjoyed this mountain trivia!


Exercises Chapter 12 

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--;
    }