while loop

# 6.11 while loop

### SYNTAX

``` while(```condition```) {     ```statements``` ; } ```OR
``` while (```condition`) `statement```; ```

### PROGRAM FLOW

1. The boolean expression condition is first evaluated. If it evaluates to true, flow continues to step 2, otherwise the loop terminates.
2. The statement(s) are executed. Flow continues with step 1.

### EXAMPLE

The following example uses the method of bisection to find an approximate solution to f(x)=0. The user must specify the function f(x), two points x1 and x2 such that f(x1) and f(x2) are of opposite sign and a precision prec > 0. When the loop terminates x1 and x2 have new values with |x1-x2| smaller than prec and f(x1) and f(x2) are of still of opposite sign. So that, assuming that f(x) is continuous, f(x)=0 has a solution between x1 and x2.
```        double x3 ;
int sign ;
if ( f(x1) >= 0 )  sign =1;
else               sign = -1 ;

while ( Math.abs(x1-x2) > prec ) {
x3 = (x1+x2)/2 ;
if ( sign*f(x3) >= 0 ) x1 = x3 ;
else                   x2 = x3 ;
}

```

### COMMON ERRORS

Don't forget the semicolon after the condition.

David Maxwell, who is still writing this, would like to hear your comments and suggestions.