Introducing the true power behind your computer ... the loop!

Consider a program that begins by reading from the user an integer less than 10. We'd probably do this using code like this:
  int n;
  cout << "Enter a number less than 10: ";
  cin >> n;
However, if the user is dumb or inattentive or malicious, they might enter a number that is greater than or equal to 10. We might want to ensure that doesn't happen (imagine our next step was to compute sqrt(10 - n), for example), so we might extend our code to something like this:
  int n;
  cout << "Enter a number less than 10: ";
  cin >> n;

  if (n >= 10)
  {
    cout << "Enter a number less than 10: ";
    cin >> n;
  }
This looks good, however, a very dumb, inattentive, or malicious user might enter two numbers in a row that were greater than or equal to 10, thus requiring another "if" statement (like this). In fact, no matter how many of these "if" statements we put in, a sufficiently dumb, inattentive, or malicious user can get by with n having an invalid value. What we really want to say is something like
 
int n;
cout << "Enter a number less than 10: ";
cin >> n;

while n is at least 10, keep doing the following
{  
  cout << "Enter a number less than 10: ";
  cin >> n;
}

The C++ version of while n is at least 10, keep doing the following   is

while (n >= 10)

Putting that in our code fragment, we end up with:

  int n;
  cout << "Enter a number less than 10: ";
  cin >> n;

  while (n >= 10)
  {
    cout << "Enter a number less than 10: ";
    cin >> n;
  }

The while loop

The above gives us our first example of a loop. Loops allow us to tell the computer to do something over and over again. This way, a short program can do a lot of work - which is the real power behind a computer!

The syntax of a while-loop is much like the syntax of an if-statement without an else block: We have while followed by a test condition and a block of code. As long as the test condition is true ("while" the test condition is true), we keep executing the block of code (referred to as the body of the loop).

A typical task that we'll implement with a loop is finding the sum of bunch of integers input by the user. We'll assume that the user will enter a negative number to indicate that he's done inputting data. (The negative number is just an end marker - it should not be included in the sum.) The typical way of doing this is:

  
  int sum, k;
  sum = 0;
  cin >> k;

  while (k >= 0)
  {
    sum = sum + k;
    cin >> k;
  }

  cout << sum << endl;

The section of code that sets sum = 0 and reads the first value for k performs initialization, i.e. work before the loop that prepares you for the loop. The variable sum must have the value zero before the loop begins, otherwise the whole concept is blown. The first value for k needs to be read in from the user before we begin the loop, since that value will be checked in the loop's test condition. Here's the whole program.

Beware the infinte Loop!

At some point or another, we all do it. We write a program with an "infinite" loop. For example, you might accidentally write something like this?

while ( (x = 0) || (x*y < 10) )
{
  cout << "Enter x and y: ";
  cin >> x >> y;
}
Can you spot the problem?
  1. I accidentally wrote x = 0 rather than x == 0.
  2. Because the ||-expression is evaluated left-to-right, x is set to 0 in the first part of the ||-expression, because of my mistake (i.e., x = 0).
  3. Since x is 0, x*y is always 0, which causes the second part of the ||-expression to be true.
  4. Therefore, it's an infinite loop. The program will never end.

Problems

  1. Computing Averages - this just builds on the sum example from above.
  2. Real Ceasar-Shift Encryption - take a look at this problem from Class 5 for a description of Cesar Shift Encryption. Now we'll do it for messages of any length!
  3. Keeping track of the maximum