Java For Loop Example

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

For loop in java

For loop is basically used when you have to repeat code n number of times. Condition is tested before executing the statements.

For loop in java is better used in programming if the number of iterations is fixed. In java we have three types of for loop,

  1. Simple for loop
  2. Enhanced for loop or for-each loop
  3. Labelled for loop

Here’s the syntax and example for simple “for” loop,

Syntax

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

for(int i = 0; i < n; i++)
{
   statement..
}

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


Java labelled loop with example

Labeled for loop in java are basically used in nested loop. In nested loops, for example, if you want to put break statement in inner loop, control will come out from inner loop and continue loop outer loop.

Also Read – Do While Loop Java

By using java labelled for loop we can jump out from outer loop by writing break statement inside inner loop. Here’s the syntax and example,

Syntax

// nested loop without label    

for(.....)
{
   // code to be executed
   for(.....)
   {
      // control will come out from inner 
      // loop and continue loop outer loop
      break;
   }
   ---------------
}

// nested loop with label

for(.....) 
{ 
   // code to be executed
   for(.....)
   {
      // we can jump out from outer loop by 
      // writing break statement inside inner loop
      break;
   }
   ---------------
}

Example

// nested loop without label 
public class LabelledLoopDemo 
{
   public static void main(String[] args) 
   {
      int a,b;
      for(a = 1; a <= 5; a++)
      {
         System.out.println();
         for(b = 1; b <= 10; b++)
         {
            System.out.print(b + " ");
            if(b == 3)
               break;
         }
      }
   }
}

Output:

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

// nested loop with label
public class LabelledLoopDemo 
{
   public static void main(String[] args) 
   {
      int a, b;
      outerloop: for(a = 1; a <= 5; a++)
      {
         System.out.println();
         innerloop: for(b = 1; b <= 10; b++)
         {
            System.out.print(b + " ");
            if(b == 3)
               break outerloop;
         }
      }
   }
}

Output:

1 2 3


Enhanced for loop or for each loop in java

If you want to traverse each and every element of an array sequentially use for each loop. For each loops are nothing but enhanced for loop.

Also Read – Java If Else

For each loop in java is very compact and easy to read since we do not need increment value and subscript notation. Let’s see it’s syntax and an example,

Syntax

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

Example

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

Output:

2
4
6
8
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 for loop in java with example. 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