For Loop in Java – An Ultimate Guide to Master the Concept

Fed up of doing the same thing repeatedly? So, try For Loop in Java or also called as Entry Control Loop. This loop is a concise version of while loop. In our previous article Loops in Java we have studied a general introduction about “For loop”, but here we will explore different scenarios in which it is used.

To understand any iteration, you should know its execution process. Here, we will uncover the execution process of For loops with example and discuss how programmers are manipulating “for loops in Java”.

What is Java For Loop?

For loop in Java is an entry controlled loop that allows a user to execute a block of a statement(s) repeatedly with a known number of times on the basis of a specified condition.

Syntax –

for(initialization; boolean expression; update statement) 

{

 //Body 

}

Execution of For Loop in Java

  • Initialization: Here, the value of the variable is initialized.
  • Condition: The condition is checked whether it is true or false. If it is true then the loop will be executed, else it will be terminated.
  • Increment/Decrement: After the condition is checked the control will go inside the body of loop and after executing the statement, it will increment or decrement the value.

Try your hands on Decision Making in Java 

Manipulating For Loops in Java

In this way, many programmers manipulate the for loop-

Manipulating For Loops in Java

1. Providing expression in for loop is a must

Syntax –

for ( ; ; )
is similar to
while(true)

Example –

public class Example1
{
    public static void main(String[] args)
    {
        for( ; ; )
        {
            System.out.println("This is an infinite loop");
        }
    }
}

Output –

This is an infinite loop

2. Initializing multiple variables

package JavaLoops;

public class Example1 
 {
  public static void main(String args[])
  {
    int iterator1=2; 
        for(long iterator2=0,z=0;iterator1<10 && iterator2<10;iterator1++,iterator2++)
          { 
        	System.out.println("Value of is iterator2 is"+iterator2);
         }
        System.out.println("Value of iterator1 is"+iterator1);
  }
}

In the above code, the iterator1 variable is initialized two times that means we can initialize any variable multiple times and the last initialized value would be the value of that variable.

It’s time to enhance your fundamental skills with Variable in Java

Output-

Example

3. Redeclaration of a variable in an initialization block

package JavaLoops;

public class Example2
  {
    public static void main(String args[])
    {
      //iterator is integer
      int iterator1=5;
      //redeclaring iterator as long will not work
          for(long iterator2=0,iterator1=1;iterator1<10;iterator1++)
            {
  
          System.out.println("Value of iterator1 is"+iterator);
    }

    }
  }

Output-

Example

Here, iterator1 and iterator2 are declared in a different way-

package JavaLoops;

public class Example3
  {
    public static void main(String args[])
    {
      int iterator1=0;
      long iterator2=10;
          for(iterator2=0,iterator1=1;iterator1<10;iterator1++)
            {
  
          System.out.println("Value of iterator1 is"+iterator1);
    }

    }
  }

Output-

Example

It was already initialized to zero as integer and is being re-declared in the Java For loop with data type long.

4. Variables declared in the initialization block must be of the same type

public class Example4 
{
   public static void main(String[] args)
     {
       for (long y = 0, x = 1; x < 5; x++)
       {
        System.out.println(x+"");
      }
     }
}

Output-

Example

It must have a common initialization block too.

Recommended Reading – Access Modifiers in Java 

5. Variables in the loop are accessible only within

public class Example5 {
  public static void main(String[] args)
  {
  int x,y;
     for(x = 0, y = 0; x < 3 && y < 3; x++, y++)
         {
         System.out.println(y + " ");
         }
    System.out.println("Value of x is " +x);
     }
}

Output-

Example

Summary

There is no standard way for Java for loops. You are not only limited to the basic syntax of for loop. With the above manipulations, we understood that there are different ways in which we can run for loop and get the desired results accordingly.

Hope, we help you to learn Java for loop. Please share your queries and suggestions in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.