Monday, November 18, 2013



1) The Do-While Loop in c++


We have already gone through The While Loop and The For Loop. Now its time for The Do-While Loop.

/* A c++ program example that uses do-while loop to display number from 0 to 9 */


// Program 1

#include <iostream>

using namespace std;

int main ()
{
    int i = 0;
    do
    {
        cout << i << endl;
        i++;
    } while (i < 10);
    return 0;
}



  • The do-while loop works just like the for loop and while loop but with one exception. 
  • Unlike the for loop and while loop, the do-while loop will execute at least once. 
  • The for loop and the while loop checks the condition and then the body of loop executes but in case of do-while, the body is executed first and then it checks the condition. 

                                              Program 2

#include <iostream>

using namespace std;

int main ()
{

    // The while loop
    int i = -1;
    while (i != -1)
    {
        cout << "Inside the while loop. " << endl;
        cout << "Please enter a number or -1 to quit: ";
        cin >> i;
    }

    // The for loop
    int j = -1;
    for (; j != -1; )
    {
        cout << "Inside the for loop. " << endl;
        cout << "Please enter a number or -1 to quit: ";
        cin >> j;
    }

    // The do-while loop
    int k = -1;
    do
    {
        cout << "Inside the do-while loop. " << endl;
        cout << "Please enter a number or -1 to quit: ";
        cin >> k;
    } while (k != -1);

    return 0;


  • When you run the above program, only the body of do-while gets executed and others do not. 
  • The initial value is set to -1 and the condition is such that the value should not be equal to -1. 
  • The for loop and while loop checks the condition first and hence their body is not executed, the do-while loop executes the body first and hence it gets executed even though the condition is false, as it checks the condition after executing the body.
  • Use Do-While when you want the body of the loop to execute at least once, even if the condition is false at the start or else you could make use of the for loop and while loop.


2) The goto statement in c++


We have gone through the while loopfor loop and do-while loop

The another way to do loop in c++ is through using the goto statement. 

The goto statement was used in olden days but it is not suitable for creating modern applications. 

But since c++ supports it, you should have knowledge about it, as you may encounter a c++ source code containing goto statements, in that case you will know what it is and how it works.

How the goto statement works?


It consist of label and statements that comes under that label. 

A label is named by you and it is followed by a colon sign (:). 

During execution of program, when goto is encountered, it jumps to the statements that comes under the label specified by goto statement. 

To get a clear idea, analyze the below program example. 

/* A c++ program example that uses goto statement to display number from 0 to 9 */



#include <iostream>

using namespace std;

int main ()
{
    int i=0;

loop:
    cout << i << endl;
    i++;

    if (i<10)
        goto loop;

    return 0;
}



3) The For Loop in c++


  • Тo understand this you must first go through The While loop in c++
  • The below program, Program 1 does the same thing that the Program 1 of previous chapter The While Loop does. 
  • An integer i is declared and then three expression of the loop: initializer, loop-test and counting expression, all are passed as parameters in for loop. 
  • The variable i is initialized to 0 then it will test the condition whether the value of i is less than 10. If the condition is true, the body of loop will execute. 
  • The incremental statement will be last statement to be executed by the for loop.

/* A c++ program example that uses for loop to display number from 0 to 9 using incrementation in the loop */


                                        Program 1

#include <iostream>

using namespace std;

int main ()
{
    int i;

    for (i=0; i<10; i++)
    {
        cout << i << endl;
    }

    return 0;
}

If you are just going to use the variable within the body of loop, then better declare it in the for loop itself. 

This will make your program more reliable. The below programs declare variable in the for loop.


No comments:

Post a Comment