- A control structure which causes a sequence of statements to be executed repeatedly
while (Expression)
|
- The expression in the parentheses is a logical (Boolean) expression
- If the value of the expression is true, execute Statement and then go back and test the expression again
- If the value of the expression is false, Statement is skipped and execution continues at the statement immediately following the loop
- The body of the loop can be a block of statements
- Block of statements must be enclosed in { }
// Computes the sum 1+2+3+...+n
#include <iostream.h> void main()
{
int i, sum; cout << "Enter n\n";
cin >> n;
sum=0;
i=1;
while (i <= n)
{
sum=sum + i;
++i;
}
cout << "The is" << sum;
}