Kamis, 12 Januari 2012

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.

// Program 1

#include

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

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.


View the original article here



Peliculas Online

Related Posts:

  • Selection Statement (if-else if-else) in c++ To understand this chapter you should know about Comparison Operators which is explained in previous chapter. Selection statements are very important in programming because we make decisions using it. // Program 1#include #i… Read More
  • Repetition Statement: The For Loop in c++ To 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 express… Read More
  • 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.// Program 1#include using namespace std;int main (){int i = 0;do{cout i endl;i++;} while (i 10);return 0;}The do-while loo… Read More
  • The goto statement in c++We have gone through the while loop, for 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 appl… Read More
  • Repetition Statement: The While Loop in c++ The fundamental to programming are control statements. You must learn to have good command over control statement, if you want to program. Sequence, selection and repetition are three types of control statement. It specifies… Read More

0 komentar:

Posting Komentar