Java for loop example

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

For loop is used when you have to repeat a task “n” number of times. That is, we know to print some statement.

For loop has four parts, variable declaration, condition, statements and increment/decrement variable.

For loop is of three types,

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

Syntax

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

Java For Loop Example

NOTE:

  • 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.
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 for loop

Labeled for loop is nothing but naming a for loop. We put label before for loop. It is used in nested for loop.

Also read – do while loop java

By using java labelled for loop we can continue or break from particular loop (inner/outer).

Syntax

label:
for(initialization; condition; increment/decrement)
{
   // code
}
public class LabelForLoop
{
   public static void main(String[] args)
   {
      xx: 
      for(int a = 5; a <= 7; a++)
      { 
         yy: 
         for(int b = 5; b <= 7; b++)
         { 
            if(a == 6 && b == 6)
            { 
               break xx; 
            } 
            System.out.println(a + " " + b); 
         } 
      } 
   }
}

Output:

5 5
5 6
5 7
6 5

// example for breaking inner for loop
public class LabelForLoop
{
   public static void main(String[] args)
   {
      xx: 
      for(int a = 5; a <= 7; a++)
      { 
         yy: 
         for(int b = 5; b <= 7; b++)
         { 
            if(a == 6 && b == 6)
            { 
               break yy; 
            } 
            System.out.println(a + " " + b); 
         } 
      } 
   }
}

Output:

5 5
5 6
5 7
6 5
7 5
7 6
7 7


Enhanced for loop or for each loop

For each loops are nothing but enhanced for loop. For each loop traverse the elements or each element of an array sequentially.

Also read – java if else

Syntax

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

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

Infinite for loop in java is called infinite, when condition inside for loop is always evaluated as true or just use two semicolons in for loop.

Syntax

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

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…………….


Related Posts