0
Posted December 12, 2009 by Spyros in C/C++ Programming
 
 

5. C++ Looping Structures

C++
C++

Whenever we need to fill in an array, multiply a variable against a value multiple times or generally want to do the same stuff over and over, we use the C++ looping structures. They help us repeat the commands that we want to execute. We specify a set of commands to be executed according to a certain condition that we also define. While this condition is evaluated as true, the set of commands keeps executing, while when it becomes false, the looping structure ends. In general, the checking condition of a looping structure consists of a counter, that counts the number of times that the set of commands was executed. There are 3 different looping structures :

1. The for loop


for (counter = starting value; counter = final value; counter = repetition step)
{

// set of commands here

}

This is the most widely used and most versatile looping structure. In this one, everything gets initialized inside the parenthesis and therefore we do not need to write the values of the counter and the repetition step separately. This is an example that illustrates that more closely :


#include <iostream.h>
#include <conio.h>

void main()
{
    int i,m=0;
    for (i=0; i<=6; i+=2)
    {
        m+=2;
       cout<<m<<endl;
    }

    getch();
}

When the program reaches the looping structure, it checks the condition inside the parenthesis. Having initialized counter i as 0, the program checks whether the counter is smaller or equal to 6. If the condition is true, the commands inside the brackets are executed. When all the commands get executed, the program rechecks the condition inside the parenthesis and increases the counter depending on the initial value that we have provided. In this example, the value is 2. If this condition is true again, all the commands are executed again. Once more the counter gets increased and gets the value of 4. The new check on whether this value is smaller or equal to 6 is true and the commands execute again. When the counter gets 6 as its value and the commands execute, the counter then gets the value of 8. The new check shows that 8 is bigger than 6 and the condition now returns false, thus finishing the loop.

2. The While Loop


counter = initial value;
while (counter = final value)
{
    set of commands ..
    counter = repetition step;
}

This looping struct does the exact same thing that for does. The only difference is that for this one we need to initialize the counter in a different manner when it comes to the loop and the repetition step. We need to put that inside the brackets and have it run along with the other commands. In the condition, inside the brackets, we input the value of the counter that when acquired, the loop ends. This means that while the condition is true, the loop goes on with the execution :


#include <iostream.h>
#include <conio.h>

void main()
{
    int i=0,m=0;

    while(i<=6)
    {
        m+=2;
        cout<<m<<endl;
        i+=2;
    }

    getch();
}

At first, we need to initialize the counter. It is the initial value that the counter has. Then, we write the final value of the counter inside the bracket. When this value gets surpassed, the loop ends. As with the previous for loop, inside the brackets reside the commands to be executed. Moreover, inside them we write the repetition step. The order of the commands is important because depending on the final value of the counter, the set of commands can be executed as many times as we would like them to. When the condition becomes false, the loop ends. In the example that follows, the commands are going to be executed 4 times :

Values of   i       Results

0                                   2

2                                   4

4                                   6

6                                   8

8                              the loop ends

3. The Do While Loop


counter = initial value;

do
{
    set of commands ..
    counter = repetition step;
}

while (counter = final value);

As in the previous loop, we need to initialize the counter and include the repetition step as well as the actual commands to be executed. Also, the looping structure will be executed as long as the condition is true. However, there is a big difference when compared to the previous structures. The commands are going to be executed at least once. This happens because in this looping structure, the commands are executed first and the condition is checked after that. Here’s an example of that :


#include <iostream.h>
#include <conio.h>

void main()
{
    int i=0,m=0;
    do
    {
        m+=2;
        cout<<m<<endl;
        i+=2;
    }
    while(i<=6);

    getch();
}

The loop executes 4 times as well. The commands run first and then the counter gets increased. In the end, the condition is checked. When the counter becomes 8, the commands get executed and then the condition specified that the loop ends, since it returns false.

To conclude, the important things to keep in mind for looping structures are :

1. Correct usage of the different structs

2. Check the initial value of the counter

3. Check the final value of the counter

4. Check the set of commands to be executed

Thanx for reading that, make sure that you also read the fourth part about C++ Introduction to Functions.

This was a guest post by a good personal friend of mine under the name Black Shadow.


Spyros