Looping Statements in Java

Introduction

In Java looping statements are used when we want to repeat something again and again. More precisely, a loop executes the same thing repeatedly until it meets the terminus. Looping makes the work relatively simple for us. Generally we have three looping statements in Java:

  1. for (simple and enhanced)
  2. while
  3. do while

The for loop

When we want to repeat specific code multiple times we use a for loop. We mainly use a for loop when we know the number of times we need to repeat the specific code. The for loop consists of initialization of a variable, testing of a condition and updating the value. Initialization is executed first and it will be executed only once. Next, the condition is tested. If it is true then the loop will execute and if it is false then it will stop executing. Then the value is updated for the next iteration. Now the flow will not go to the initialization, it will go to the second step, in other words the testing of the condition and will do the same until the termination is met.

Syntax:

for (initialization; condition; update)

{

   Statements...

}

 

Example 1:

 

package myclass1;

class Myclass1

{

   public static void main(String args[])

   {

      System.out.println("increment");

      for(int i = 1; i < 10; i++)

      {

         System.out.print("i : " + i );

         System.out.print("\n");

      }

   }

}

 

Output:


for increment loop.jpg

 

Example 2:

 

package myclass1;

class Myclass1

{

   public static void main(String args[])

   {

      System.out.println("decrement");

      for(int i = 20; i > 10; i--)

      {

         System.out.print("i : " + i );

         System.out.print("\n");

      }

   }

}

 

Output:


for loop decrement.jpg

 

The enhanced for loop

 

The enhanced for loop was first introduced in Java 5. It is generally used in arrays. Here the declaration is the declaration of the type of the elements of the array that you are accessing. The expression provides the array in which you want to access the elements.

 

Syntax:

 

for (declaration : expression)

{

   Statements...

}

 

Example:

 

package myclass1;

class Myclass1

{

   public static void main(String args[])

   {

      int [] array = {111, 222, 333, 444, 555};

      for(int i : array )

      {

         System.out.print( i );

         System.out.println("\n");

      }

   }

}

 

Output:


enhanced for loop.jpg

 

The while loop

The while loop also repeats specific code again and again until the condition is false. It executes when the condition is true and will stop when it is false. In a while loop the first condition is checked. If it is true then it will execute. Then it will return to check the condition again. The same process will continue until the condition is false.

Syntax:

while (condition)

{

   Statements...

}

Example:

package myclass1;

class Myclass1

{

   public static void main(String args[])

   {

      int i = 15;

      while( i < 25 )

      {

         System.out.print("i : " + i );

         i++;

         System.out.print("\n");

      }

   }

}

 

Output:


while loop.jpg

 

The do-while loop

 

The do while loop is similar to while loop. It will always execute at least once. The do while loop checks the condition at the end of the loop. First it executes the statements and then checks the condition. If the condition is true then it will again execute the statements. This entire process will continue until the condition becomes false.

 

Syntax:

 

do

{

   Statements...

}

while (condition);

 

Example:

 

package myclass1;

class Myclass1

{

   public static void main(String args[])

   {

      int i = 990;

      do

      {

         System.out.print("i : " + i );

         i++;

         System.out.print("\n");

      }

      while( i < 1000 );

   }

}

 

Output:


do while loop.jpg

 

Comparison of for, while and do while

 

In a for loop we know how many times we need to repeat the specific code, but in a while and a do while loop we do not know. If the condition is false in a for and a while loop then they will not execute because in these loops the condition is checked first and then the statements are executed. But in a do while loop if the condition is false then it will also execute onece because in a do while loop the statements are executed first and then the condition is checked.

 

Example:

 

package myclass1;

class Myclass1

{

   public static void main(String[] args)

   {   

      for (int f = 5; f < 0; f++)

      {

         System.out.print("f : " + f );

         System.out.print("\n");

      }

       System.out.println("nothing");

      int w = 5;

      while (w < 0)

      {

         System.out.print("w : " + w );

         System.out.print("\n");

         w++;

      }

       System.out.println("nothing here too");

      int d = 5;

      do

      {

         System.out.print("d : " + d );

         System.out.print("\n");

         d++;

      }

      while (d < 0);

   }

}

 

Output:


combination of loops.jpg

 

Summary

 

This article has introduced you to the looping statements in Java. It also covered the syntax and examples of these looping statements.

Up Next
    Ebook Download
    View all
    Learn
    View all