Thursday, 16 May 2013

WHILE AND DO WHILE LOOP

//WHILE LOOP 
#include <iostream>

using namespace std; // So we can see cout and endl

int main()
{ 
  int x = 0;  // Don't forget to declare variables
  
  while ( x < 10 ) { // While x is less than 10 
    cout<< x <<endl;
    x++;             // Update x so the condition can be met eventually
  }
  cin.get();
}
//DO WHILE LOOP 
#include <iostream>

using namespace std;

int main()
{
  int x;

  x = 0;
  do {
    // "Hello, world!" is printed at least one time
    //  even though the condition is false
    cout<<"Hello, world!\n";
  } while ( x != 0 );
  cin.get();
}
 
The main difference between WHILE & DO WHILE LOOP is that while loop will execute
only if the condition is true else it will never execute, AND DO WHILE LOOP will
execute for at least one time even if the condition is false.

No comments:

Post a Comment