## Free Data Structures and Algorithms Course

### while, do-while, for and for-each loop statements

Lets move ahead with my Java How to Program series to Iteration Statements. Generally, Iteration statements or looping statements are statements which are executed repeatedly based upon a boolean condition. Here boolean condition is also called as loop condition because it helps to determine when to terminate the loop. The code executed in each iteration is generally called as loop body. Basically a loop body may contain single statement or block of statement to execute.
Java programming language provides mainly three iteration statements such as :
1. for statement
2. while statement
3. do-while statement
Generally all three iteration differ only in order in which they are executed. Mainly in, for statement and while statement loop condition is tested before executing loop body. In do-while loop condition is tested after executing loop body. Other than three basic iteration statement there is one enhanced for loop called as for-each loop. Generally the test condition should always be placed in such a way that loop terminated after a few iterations. If test conditions are placed wrongly the execution might enter into infinite loops. The overall looping process involves 3-4 steps. Let us examine each of them one-by-one :
1. Initialization of counter variable to generate a counter for number of loops we want to follow.
2. Having execution of the various statement in the loop.
3. Testing the condition to proceed for the loop.
4. At last incrementing the counter and following all steps above again.
Now we will discuss some of the widely used loop statements in Java :

The while loop
The simplest loop is the while loop. The general format of the while loop is as follows:

```initialization;

while ( test condition) {

statements;

increment;
}```

while loop flow chart :

The while loop working
The basic format is like initially initialization of a counter variable is done. This initialization helps to stop the iteration. After the initialization of counter variable test condition is evaluated. This evaluation of test condition is either true or false. Generally if test condition is true the loop body (statements) are executed. If not the while loop gets exit and next statement following it gets executed. Generally after statements are executed there comes optionally an incremental statement . The increment is necessary because it helps loop to get terminate. If this increment step is not present then while loop enters infinite loop. Generally we use counter variable in the test condition and even in the increment step. It is done so because each time loop executes test condition is tested with counter variable and we can make test condition true by using increment step at the bottom.

The do-while loop The do-while loop is a special looping statement. Generally significance of having this loop is to get atleast one time body statements to execute. Here looping follows some different pattern as compare to other loops. Lets look at the general format of the do-while loop :
```initialization;
do{
statements;
increment;
}
while ( test condition); ```
The do-while working The while loop makes a test condition first before execution of the statements in the body of loop. If the condition is true then only body in loop gets executed or else while loop gets executed. But in do-while loop body of loop gets executed and then test condition is evaluated for the next turn. That means body of the do-while loop will be executed once before any test condition is tested. So for having a loop which we think that it must execute body at least once we use do-while loop. The do keyword performs the execution and while keyword test the condition if test is true the control transfer to the do block again . If test condition is false the control exits do-while loop and next section of code gets executed.

The for loop The for loop is highly and most loved looping statement for the programmers. Lets look at the general format of the for loop :
```for ( initialization; test condition; increment )
{
statement;
} ```

The for loop working
The basic format of for loop is like initially initialization of a counter variable is done. This initialization helps to stop the iteration. After the initialization of counter variable test condition is evaluated. The counter variable is tested against the test condition. This evaluation of test condition is either true or false. Generally if test condition is true the loop body (statements) are executed. If not the for loop gets exit and next statement following it gets executed. Generally after statements are executed than there is an incremental statement . The increment statement increments or say decrements values of counter variable which helps loop to get terminate. If this increment step is not present then for loop enters infinite loop.

The for-each loop The for-each loop is an enhanced for loop that works similarly as for loop but it helps programmer in many ways. Generally it helps programmer when iterating on Collections. Typical example could be iterating over array elements without using array indexes. Lets look at the general format for the for each loop :
```for( Type Identifier : Expression )
{
statements;
} ```
The for-each loop working Generally if we look at the general format of for each loop we see that it has type identifier and expression. Generally the type is the data type or the object which is been used. Identifier following it is name of the variable. Lastly, expression used must be a instance of array or instance of java.lang.Iterable interface. Suppose we want to loop over array, two things we can do : 1. Either iterate array by indexes using for loop. 2. Using for-each loop and just using expression of the for-each loop to be array variable name.

© 2021 Learn Java by Examples Template by Hubberspot