GuidePedia

0
The for loop does something we know in advance that how many times a loop has to be executed.
But if you don’t know that how many times your loop is going to be run then while loop is the choice.

Syntax

While(condition)
{
     Statement(s);
}

The flow diagram for it is:


Let’s have a look at an example:


#include <iostream>
using namespace std;

int main(){
int n = 99;     // make sure n isn’t initialized to 0
while( n != 0 )   // loop until n is 0

cin >> n;      // read a number into n
cout << endl;

return 0;

}


Here’s some sample output. The user enters numbers, and the loop continues until 0 is entered, at which point the loop ends and the program is terminated.

Output:

1 27 33 144 9 0
                 


Post a Comment

 
Top