Java - Loop control

Loop is very common control flow statement in programming languages such as java. We are going to describe the basics of “java loop”. In this post, we will learn various ways to use loop in day-to-day programming habits.

There may be a situation when we need to execute a block of code several number of times, and is often referred to as a loop

There are four types of loops:

  1. 1. For loop
  2. 2. For each loop
  3. 3. While loop
  4. 4. Do..While loop

For Loop

It is structured around a finite set of repetitions of code. So if you have a particular block of code that you want to have run over and over again a specific number of times the For Loop is helpful.

Syntax:

                
			for(initilization; conditional expression; increment expression)

{ //repetition code here }

 

Example:

                
			public class Example {
			      public static void main(String args[]) {
			           for(int x = 50; x < 55; x++) {
                                       System.out.println("Value of x : " + x );
			           }
			      }

			}

Output:

Value of x : 50;

Value of x : 51;

Value of x : 52;

Value of x : 53;

Value of x : 54;

 

For each Loop

This loop is supported from Java 5.

For each loop is mainly used for iterate the Array, List etc.

 

Syntax:

                
			for(declaration : expression)
			{
			    //Code Here
			}

 

Example:

                
	public class Example {

   public static void main(String args[]){

      List list = new ArrayList();
      list.add(10);
      list.add(20);
      list.add(30);
      System.out.print("List Value = ");
      for(int x : list){
         System.out.print( x );
         System.out.print(",");
      }
      String [] names ={"abc", "xyz", "test", "example"};
      System.out.println("String Array value = ");
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}
		

 

Output:

List Value = 10,20,30

String Array value = abc,xyz,test,example

 

While Loop

Another looping strategy is known as the While Loop. The While Loop is good when you don’t want to repeat your code a specific number of times, rather, you want to keep looping through your code until a certain condition is met.

Syntax:

                
			while(Boolean_expression)
			{
			   //Repetition Code Here
			}

 

Example:

                
		
public class Example {

   public static void main(String args[]) {
      int x = 50;

      while( x < 55 ) {
         System.out.println("Value of x : " + x );
         x++;
      }
   }
}

Output:

Value of x : 50

Value of x : 51

Value of x : 52

Value of x : 53

Value of x : 54

 

Do..While Loop

This type of loop is used in very rare cases because it does the same thing as a while loop does, except that a do..while loop is guaranteed to execute at least on time.

 

Syntax:

                
			do
			{
			   //Code Here
			}while(Boolean_expression);

 

Example:

                
			
public class Test {

   public static void main(String args[]){
      int x = 50;

      do{
         System.out.println("Value of x : " + x );
         x++;
      }while( x < 50 );
   }
}

 

Output:

Value of x : 50

 

Above example first execute code inside loop and then check condition that’s why it’s display 50.