Java – Looping Statements For, While and Do-While


What is a looping statement ?

Looping statement are the statements executes piece of code repeatedly until the condition is reached.

Why java uses looping statements ?

When there is a need to execute the code for several number of times, java uses looping statements.

What are the advantages of the looping statements ?

1. Reduces the length of the code.

2. Uses less memory space.

What are the looping statements in java ?

1. For Loop

2. While Loop

3. Do-While Loop

1. For – Loop

Its is a control flow statement which allows the code to be executed repeatedly until the iteration numbers are reached.

What are the types of for loop ?

There are 3 types of for loops


1. For Loop (Simple)

2. For-each Loop

3. Infinitive For Loop

1. For loop (Simple) :

For loop works on index basics.

Syntax :

for(initialisation; condition; increment/decrement){  
//code to be executed  
}  

1. Initialisation part

2. Conditional part

3. Increment/ Decrement part

1. Initialisation part

Here we can declare and initilise the loop control variable.

2. Conditional part

Here the conditions are checked ,if the condition is true the loop is executed, and if the condition is false then the loop does not execute and the control flow goes outside of the for loop.

3. Increment/ Decrement part

Explanation :

For loop is executed with reference to the initial value mentioned in Initialisation part and executes the condition in the conditional part, then uses the Increment/ Decrement part to satisfy the condition by iterating the rows until the condition is satisfied 

Example 1 :

public class Eg1{
public static void main(String args[])
{
        for(int i=0;i<10;i++)
        {
        System.out.println("The value of i =" +i);
        }
}
}

Output :

The value of i =0

The value of i =1

The value of i =2

The value of i =3

The value of i =4

The value of i =5

The value of i =6

The value of i =7

The value of i =8

The value of i =9

2. For Each Loop :

For Each loop works on element basics.

Its is used to iterate the values from array or collection in java.

Syntax :

for(Type var:array){  
//code to be executed  
} 

Example 1 :

public class Eg2{
        public static void main(String args[])
        {
                int a[] = {0,1,2,3,4,5,6,7,8,9,10};
                for(int i:a){
                        System.out.println("The value of i = "+i);
                }
        }
}

Output :

The value of i = 0

The value of i = 1

The value of i = 2

The value of i = 3

The value of i = 4

The value of i = 5

The value of i = 6

The value of i = 7

The value of i = 8

The value of i = 9

The value of i = 10

### Applying Simple For loop for Array value iteration.

Example 2 :

public class Eg2{
        public static void main(String args[])
        {
                int a[] = {0,1,2,3,4,5,6,7,8,9,10};
                for(int i=0;i<a.length;i++)
                {
                System.out.println("The iteration of array values = "+a[i]);
                }
        }
} 

Output :

The iteration of array values = 0

The iteration of array values = 1

The iteration of array values = 2

The iteration of array values = 3

The iteration of array values = 4

The iteration of array values = 5

The iteration of array values = 6

The iteration of array values = 7


The iteration of array values = 8

The iteration of array values = 9

The iteration of array values = 10

3. Infinitive For Loop :

If Initialisation ,Conditional and Increment/Decrement parts are empty, It will become Infinitive Loop.

Syntax :

for(;;){  
//code to be executed  
}  

Example 1 :

public class Eg3{

        public static void main(String args[])
        {
                for(;;)
                {
                        System.out.println("This is infinite loop runs infinity times");
                }
        }
}

Output :

This is infinite loop runs infinity times

This is infinite loop runs infinity times

This is infinite loop runs infinity times

This is infinite loop runs infinity times

This is infinite loop runs infinity times

This is infinite loop runs infinity times

2. While Loop

Its is also a control flow statement which executes the code repeatedly untill the boolean condition is satisfied.

It checks first the boolean condition and based on that success, the flow of the code is controlled.

1. Simple While Loop :

Syntax :

while (boolean condition)
{
   statements...
}

Example 1 :

public class Eg1{

public static void main(String args[])
{
int a = 0;
while(a<5){
 System.out.println("This while loop is executed untill the condition a<5 is satisfied a = " +a);
 a++;
}
}
}

Output :

This while loop is executed untill the condition a<5 is satisfied a = 0

This while loop is executed untill the condition a<5 is satisfied a = 1

This while loop is executed untill the condition a<5 is satisfied a = 2

This while loop is executed untill the condition a<5 is satisfied a = 3

This while loop is executed untill the condition a<5 is satisfied a = 4

2. Infinite While Loop :

Executes the statments infinity times.

Syntax :

while(true){  
//code to be executed  
}  

Example 2 :


public class Eg3{
        public static void main(String args[])
        {
        while(true)
        {
        System.out.println("Infinite while loop prints the statment infinity times");
        }
        }
}

Output :

Infinite while loop prints the statment infinity times

Infinite while loop prints the statment infinity times

Infinite while loop prints the statment infinity times

Infinite while loop prints the statment infinity times

Infinite while loop prints the statment infinity times

Infinite while loop prints the statment infinity times

3. Do-While Loop

Similar control flow statement but first it will prints the statments and then it checks for the boolean condition.

Prints the statments and then checks for the boolean condition.

1. Simple Do-While Loop :

Syntax :

do
{
    statements..
}
while (boolean condition);

Example 1 :

public class Eg2{

 public static void main(String args[])
 {
  int a = 1;
  do{
   System.out.println("Do statment printed first before checking the condition a<5 and the value of a = "+a);
   a++;
  }
  while(a<5);
 }
}

Output :

Do statment printed first before checking the condition a<5 and the value of a = 1

Do statment printed first before checking the condition a<5 and the value of a = 2

Do statment printed first before checking the condition a<5 and the value of a = 3

Do statment printed first before checking the condition a<5 and the value of a = 4

2. Infinite Do-While Loop :

Executes the statments infinity times.

Syntax :

do{  
//code to be executed  
}while(true);

Example 1 :


public class Eg4{
public static void main(String args[])
{
do{
System.out.println("Do while infinite loop prints the statements infinity times");
}
while(true);
}
} 

Output :

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times

Do while infinite loop prints the statements infinity times