C++ Programming Tutorial Lesson 06: While Loops

Often in programming it is desirable to execute a section of code repeatedly while a condition is met. While loops can accomplish such a task in C++. Let's look at a while loop example that builds off of the If Else Example #2 source code from Lesson 02: If and Else Statements.

While Example #1

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int n;
   n = 1;
   while (n == 1) {
      printf("Please enter a number: ");
      scanf("%d", &n);
      if (n == 1) {
         printf("n is equal to 1!\n");
      }
      else {
         printf("n isn't equal to 1.\n");
      }
   }
   system("PAUSE");
   return 0;
}
Feel free to download the While Example #1 source code directly. This program repeatedly tests the user input from the keyboard. While n is equal to 1, the section of code encapsulated by the while statement will run repeatedly. The condition that will end the while loop is n not equal to 1. So, when the user enters a number that is equal to 1, the program will continue to ask for additional input as shown below:



However, when the user inputs, via the keyboard, a value that is not equal to 1, the program will end as shown below:



The table below describes the while loop C++ code presented in the above example.

Source Code Description & Explanation
n = 1;
This line of code sets n equal to 1 prior to the beginning of the while loop that follows. This ensures that the while loop condition of n == 1 will be satisfied.
while (n == 1) {
   printf("Please enter a number: ");
   scanf("%d", &n);
   if (n == 1) {
      printf("n is equal to 1!\n");
   }
   else {
      printf("n isn't equal to 1.\n");
   }
}
The first line is the while statement. It tests to see if n is equal to 1 upon entering the while loop. If n is equal to 1, then the loop commences with the code encapsulated by the curly braces. If n is not equal to 1, then the code encapsulated in the curly braces will not be run, and the execution of the program will jump to the code after the end curly brace of the while statement. In this case, the code in the curly braces runs the printf() function to print "Please enter a number: " onto the screen. It then runs the scanf() function to read an integer input from the keyboard that is followed by a carriage return (the return key on the keyboard). The value that was read by the scanf() function is stored in variable "n". An if else set of statements are then run. If n is equal to 1, then "n is equal to 1!" is printed to the screen. Otherwise, "n isn't equal to 1." is printed to the screen. At the end of these two if else tests, the execution of the program then loops back to the while statement, thus completing a while loop, and n is again tested against the value of 1. If n is equal to 1, then the code encapsulated by the curly braces of the while statement runs again.
system("PAUSE");
return 0;
Otherwise, if n is not equal to 1, then the execution of the program will jump to the code after the end curly brace of the while statement, at which point the system() function will run the "PAUSE" command and after which the program will end by returning a value of zero to the main function.

C++ also offers the capability to perform what are called "do while loops". The following example illustrates the functionality of a do while loop.

Do While Example #1

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int n;
   do {
      printf("Please enter a number: ");
      scanf("%d", &n);
      if (n == 1) {
         printf("n is equal to 1!\n");
      }
      else {
         printf("n isn't equal to 1.\n");
      }
   } while (n == 1);
   system("PAUSE");
   return 0;
}

Feel free to download the Do While Example #1 source code directly. This program basically has the same functionality as the while example #1 above. The "do" statement comes at the beginning of the while loop, and the "while" statement comes at the end of the loop. The test of the do while loop's condition of n equal to 1 comes at the end of the loop instead of at the beginning of the loop. This means that we don't have to initialize the value of n as we did in the while example #1 above. The do while loop will always execute at least once due to the fact that its conditional statement comes at the end of the loop.

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.