Java For Loop Example

Hey guys!! Welcome to flower brackets blog. Today you will learn java for loop example and its types.

Java for loop example

For loops java is used to repeat statements although the given condition is true.

Condition is tested before executing the statements. For loop in java is better used in programming if the number of iterations is fixed.

Syntax

for (initialization; condition; increment)
{
   statement(s).....
}

Flow diagram

Java For Loop Example

Remember following points before using for loop

  • Loop begins with an initialization expression which initializes the loop, and is executed only once.
  • Loop terminates if the termination expression evaluates to false.
  • After each iteration through the loop, increment expression is invoked.

Example

public class ForLoop
{
   public static void main(String[] args)
   {
      for(int x = 1; x <= 5; x++)
      {
         System.out.println(x);
      }
   }
}

Output:

1
2
3
4
5


Labeled for loop

Labeled for loop is useful when we use nested for loop so that we can break or continue particular for loop.

Also Read – Do While Loop Java

In labeled for loop we use label before the for loop. Let’s see its syntax,

Syntax

labelname:     

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

Example

public class LabelForLoop
{
   public static void main(String[] args)
   {
      xx: 
         for(int a = 1; a <= 3; a++)
         { 
            yy: 
               for(int b = 1; b <= 3; b++)
               { 
                  if(a == 2 && b == 2)
                  { 
                     break xx; 
                  } 
                  System.out.println(a + " " + b); 
               } 
         } 
   }
}

Output:

1 1
1 2
1 3
2 1

// Here I'm using break yy, which will break inner loop

public class LabelForLoop
{
   public static void main(String[] args)
   {
      xx: 
         for(int a = 1; a <= 3; a++)
         { 
            yy: 
               for(int b = 1; b <= 3; b++)
               { 
                  if(a == 2 && b == 2)
                  { 
                     break yy; 
                  } 
                  System.out.println(a + " " + b); 
               } 
         } 
   }
}

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3


Java enhanced for loop or foreach loop

Now, we will learn the concept of enhanced for loop or for-each loop. For-each loop is designed to iterate through arrays and collections.

Also Read – Java If Else

This loop is very compact and easy to read since we do not need increment value and subscript notation.

Syntax

for(Type var: array)
{
   // code goes here
}

Example

public class EnhancedForLoop
{
   public static void main(String[] args)
   {
      int[] numb = {2,4,6,8,10};
      for (int even : numb)
      {
         System.out.println("Even Number : " + even);
      }
   }
}

Output:

Even Number: 2
Even Number: 4
Even Number: 6
Even Number: 8
Even Number: 10


Infinite for loop

For loop can be an infinite loop since the three expressions namely initialization, termination and increment are optional.

Syntax

for ( ; ; )
{
   // code goes here
}

Example

public class InfiniteForLoop
{
   public static void main(String[] args)
   {
      for( ; ; )
      {
         System.out.println("Infinite for loop");
      }
   }
}

Output:

Infinite for loop
Infinite for loop
Infinite for loop
Infinite for loop
Infinite for loop……………. goes on


conclusion

That’s it guys. This was all about how to use for loop in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts