First is the while statement which has this syntax.
while (expression) statementAs long as the expression is true, the statement is executed.
int total =0;
int index =0;
while (total < 100)
{
index++;
total+=index;
printf("Sum of 1 to %i is %i",index,total) ;
}
Looping doesn't get much simpler than while. The most important thing to know with while loops is that the control expression is evaluated before the statement is run. If the expression is false, the statement is not run. So a while loop may never execute a statement.
Also because c allows a statement to be an expression it is possible to write complicated code like this
int c=10;c-- subtracts one from c.
int a =0;
while (c--)
{
printf("Value of a is %i",a++) ;
}
Do While
Less popular than the while statement is the do while statement. This puts the expression at the end.The syntax looks like this.
doA while loop might never execute a statement if the expression is false but a do while will always execute the statement at least once.
statement;
while (expression) ;
Here is an example of a do while loop.
int count =0;Break and Continue work equally well with for, while and do while loops.
int index =9;
do
if (value[ index] ==999)
count++;
index--;
while ( index >= 0) ;
On the next page : learn about Switch statements.

