Loops in C++ Language, Part 1


It is a kind of a loop that executes a specified number of times.The use of this type of loop is very common and occurs frequently in program.The major and important characteristics of this loop are:

  • Variable is used as a counter
  • Initialization is required for the counter
  • Increment of the counter is required
  • Final value is required for the counter

Suppose the counting type of loop is to be executed for values of   k   between  1  and max (maximum) . The following code uses the while statement to  print the values entered by the user in the same line.


  1. //program name: control.cpp
  2. #include
  3. #include
  4. #include
  5. int main ()
  6. {
  7. clrscr ();
  8. int num, k=1, max=10;
  9. while (k<=max)
  10. {
  11. cin>>num;
  12. cout<
  13. K++;
  14. }
  15. return (0);
  16. }

Run output

10 20 30 40 50 60 70 80 90 100

In function main(),  k  is the loop control variable. Initially it is set to 1 outside the loop but can be set to than 1 which must be <= to the final value, otherwise loop will not be executed. Test is performed in the iteration head before processing the loop body enclosed in curly braces. If the  test is true i.e.; k <= maximum, perfor m the loop body and repeat the process as long as it is true. The while statement always tests the condition first, in other words called the pre-test loop. If the condition is false, the loop is never executed and control transfers to the first statement outside the loop. 

You must make sure that the loop control variable is incremented (normally by 1) to change the condition so that at some point the condition becomes false to terminate the loop or you will never exit from the loop and will continue forever until the system gets exhausted or interrupt by the system command. This situation is called graphically an infinite loop.



The statement before the iteration head, k is many times will the while statement be executd.the example program control.cpp is going to be executed 20 times because of the initialization of the lcv to 1 and the relational operator <=.the same result could be done by changing the value of k to 0 and the relational operator to < (less than).the program ccontrol.c can also be done in another way. This can be done through input from the user. Consider the program ccontoll.cpp which illustrates this concept.


  1. //program name: control.cpp
  2. #include
  3. int main ()
  4. {
  5. int num,k,max;
  6.    cout<
  7.    cin>>k;
  9.    cout<
  10.    cin>>max;
  12. If (k>max)
  13.    cout<
  14. Else
  15. {
  16. While (k<=max)
  17.    cout<
  18.    cin>>num;  //assuming the input is 10, 20,…
  19. cout<
  20. k++;
  21.      }
  22. }
  23. return 0;
  24. }

Run output

Enter initial value for the loop: 1

Enter final value for the loop: 5

10 20 30 40 50

Run output

Enter initial value for the loop: 3

Enter final value for the: 2

Initial value must be <= final value

In the program ccontroll .cpp, if the user entered the initial value for the loop greater than the final value for the loop then execution of the body of the loop would not occur.

Sentinel Controlled Loops

In the sentinel controlled loops, we use a sentinel value somewhere at the time of execution. In the program scontrol.cpp, the user is asked at the keyboard (by default) to continue or exit that is a signal to the program whether to perform the loop or not. By doing that we read a value and then test it with the specified value at the iteration head as shown in program scontrol.The value read is tested with the sentinel value. If they are not equal, perform the body of the loop, otherwise exit from the loop.The program scontroll.cpp continues to red data until 99 is read in the variable val.


  1. //program name: scontrol.cpp
  2. #include
  3. #include
  4. #include
  5. const SENTINEL=999;
  6. int main ()
  7. {
  8. clrscr ();
  9. int val;
  10.    cout<
  11.    cin>>val;
  13. while (val ! = SENTINEL)
  14. {
  15. cout<
  16. cin>>Val;
  17. }
  18. return 0;
  19. }

Run output

To exit, Enter 999. Otherwise

110 20 30 40 50

It should be noted that the sentinel value (999) must be entered at the end of the data otherwise, it will ask for the data. The same result can be achieved by using 999 in the iteration head but usually the use of hard coding is not preferred. Suppose somewhere else in the program the same sentinel value has been used and you wish to set the sentinel value to some other number then you have to go through the entire program to find out the old sentinel.

Exit mobile version