CyberiaPC.com Main Page




Learning C++

Working with While/For Loops

If you want your program to execute something repetitively based on a condition that you specify, then that can be done with loops.  As the name suggests, a loop is nothing more than a block of code that goes on and on as long as the condition specified by you (the programmer) is either true or false.

There are mainly 2 types of looping structure in C++: 1. Do...While structures and 2. For structures.

First, we'll discuss Do...While loops.  This kind of structure executes a certain block of code repeatedly as long as the expression is true.  Here is how it generally looks:

     while (condition)
     {
          do this
     }

For example, the following code keeps asking the user for a positive number and only ends when the user enters a number that is positive:

     int n;
     
     cout << "enter a positive number: ";
     cin >> n;
     
     while (n < 0)
     {
          cout << "Error: Enter a positive number: ";
          cin >> n;
     }

The while loop will continue to execute until n becomes greater than 0, i.e. until it becomes positive.  Note: If the user isn't first asked to enter a value for n then the loop will not work because n will not have a value.  Also, make sure you don't enter a semicolon (;) on the line that contains the while statement because that will cause the loop to become what is known as an infinite loop.

Second, we have For loops.  A For loop is used when a block of code is to be repeated a set number of times.  It is much more efficient to use in this context and can be very useful when dealing with numbers.  It does the same function as while loops.  Here is how a For loop likes like:

     for (initialization; condition; increment)
     {
          do this
     }

Initialization: Performed only once when the loop is first executed.  It declares an variable that will be used for comparison.

Condition: Performed after each loop (iteration).  This is checked against the initialized variable.

Increment: Usually a counter that is incremented or decremented.  This value, in most cases, determines when the loop ends.

Here is an example:

     for (int i=0; i<10;i++)
     {
          cout << "Value reached: " << i << endl;
     }

This code segment first sets i to zero.  Then, as long as i is less than ten, the message Value reached: (value of i) is displayed, each on its own line.  After the message is displayed each time, i is incremented by 1 (That's what '++' does).  Here is what the program's output will look like:

     Value reached: 0
     Value reached: 1
     Value reached: 2
     Value reached: 3
     Value reached: 4
     Value reached: 5
     Value reached: 6
     Value reached: 7
     Value reached: 8
     Value reached: 9

There are all sorts of things that you can do with loops, like counting or summing.  Note that if you want to execute a code that adds the value 1 to a current variable, write the following line:

     counter = counter + 1;

The same like can be written as:

     counter += 1;

or if you're multiplying:

     counter *= 1;

Note that counter is a variable declared as integer.

You may also wish to use the clscr() function, which clears the screen.  This is available from the conio library.  Just add the header file #include<conio.h> at the top of your code and the function will be accessible.

Finally, you've got the break statement.  You can use it whenever you want to quit a loop and continue on with the program's code.

More exercises and sample programs on While/For loops are provided in C++ CoderCheck it out!

{Go Back To C++ Lessons Page}