Friday, 9 March 2012

The While Statements in C++

The while statement examines or evaluates a condition. The syntax of the while statement is:
while(Condition) Statement;

Flowchart: while

To execute this expression, the compiler first examines the Condition. If the Condition is true, then it executes the Statement. After executing the Statement, the Condition is checked again. AS LONG AS the Condition is true, it will keep executing the Statement. When or once the Condition becomes false, it exits the loop.
Here is an example:
int Number;

while( Number <= 12 )
{
 cout << "Number " << Number << endl;
 Number++;
}
To effectively execute a while condition, you should make sure you provide a mechanism for the compiler to use a get a reference value for the condition, variable, or expression being checked. This is sometimes in the form of a variable being initialized although it could be some other expression. Such a while condition could be illustrated as follows:
Second while Flowchart


An example would be:
#include <iostream>
void main()
{
 int Number;// = 0; 

 while( Number <= 12 )
 {
  cout << "Number " << Number << endl;
  Number++;
 }

 getch();
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Web Hosting Coupons