Tuesday, October 25, 2011

Repetition Statement: The Do-While Loop in c++


Repetition Statement: 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.

/* A c++ program example that demonstrates how do-while loop distinguishes from the for loop and while loop */


// 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.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

6 comments:

  1. file in turbo c
    http://fahad-cprogramming.blogspot.com/search/label/CH%205%3AHow%20manage%20a%20C%20program%20file%20in%20Turbo%20C

    ReplyDelete
  2. A very good and interesting blog that i have come across, thanks for sharing a valuable post.

    ReplyDelete
  3. Yes, I agree with both of commentators that this blog is very interesting! It's excellent that you do love programming and would like to teach your friends! Our custom essay writing services are great for your other academic purposes!

    ReplyDelete
  4. Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily.
    c++ programming

    ReplyDelete
  5. It would not be fair if we compliment all the blog writings but not the author.
    wifexited

    ReplyDelete