AppDividend
Latest Code Tutorials

Java For Loop: Iteration in Java – Complete Guide

Java Loops execute the set of statements repeatedly until the particular condition is satisfied. Looping in the programming languages is the feature that facilitates the execution of the set of instructions/functions frequently while some condition evaluates to true. There are the following three types of loops in Java.

  1. For loop
  2. While loop
  3. Do-while loop

Java for loop

Java for loop provides a concise way of writing the loop structure. Unlike the while loop, a for statement consumes an initialization, condition, and increment/decrement in one line, thereby providing the shorter, easy to debug structure of looping in Java Programming. If several iterations are fixed, it is recommended to use the for loop.

Syntax

See the following syntax.

for (initialization condition; testing condition; increment/decrement)
{
    statement(s)
}
  1. Initialization condition: Here, we initialize a variable in use. It marks the start of the for loop. Already declared variable could be used or the variable that can be declared, local to loop only.
  2. Testing Condition: It is used for testing an exit condition for the loop. It must return the boolean value. It is also the Entry Control Loop, as the condition is checked before executing the loop statements.
  3. Statement execution: Once a condition is evaluated to true, the statements in a loop body are executed.
  4. Increment/ Decrement: It is used for updating a variable for the next iteration.
  5. Loop termination: When a condition becomes false, the for loop terminates, marking the end of its life cycle.

See the following code example.

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

See the following output.

➜  java javac Loop.java
➜  java java Loop
Value of x is:19
Value of x is:20
Value of x is:21
Value of x is:22
➜  java

Java Nested For Loop

If we have the for loop inside another loop, it is known as the nested for loop. The inner loop executes completely whenever the outer loop executes. See the following code example.

class Loop {
  public static void main(String args[]) {
    for (int x = 19; x <= 22; x++) {
      for (int y = 19; y <= 22; y++) {
        System.out.println("Value of x and y are:" + x + " " + y);
      }
    }
  }
}

See the following output.

➜  java javac Loop.java
➜  java java Loop
Value of x and y are:19 19
Value of x and y are:19 20
Value of x and y are:19 21
Value of x and y are:19 22
Value of x and y are:20 19
Value of x and y are:20 20
Value of x and y are:20 21
Value of x and y are:20 22
Value of x and y are:21 19
Value of x and y are:21 20
Value of x and y are:21 21
Value of x and y are:21 22
Value of x and y are:22 19
Value of x and y are:22 20
Value of x and y are:22 21
Value of x and y are:22 22
➜  java

Java for-each Loop

Java also includes another version of the for loop introduced in Java 5. Enhanced the for loop provides a more straightforward way to iterate through the items of a collection or array. However, it is inflexible and should only be used when required to iterate through the items sequentially without knowing an index of the currently processed item.

Also, note that an object/variable is immutable when the enhanced for loop is used, i.e., it ensures that the values in an array can not be modified, so it can be said as the read-only loop where you can’t update the values as opposed to other loops where the values can be changed.

We recommend using this form of the for statement instead of a general form whenever possible.

The for-each loop traverses an array or collection in Java language. It is easier to use than simple for loop because we don’t need to increment the value and use the subscript notation.

It works based on an item and not an index basis. It returns items one by one in the defined variable.

See the following syntax of the for-each loop.

for(Type var: array){  

   //code to be executed  

}

See the following code example.

class Loop {
  public static void main(String args[]) {
    int arr[] = { 18, 19, 21, 29, 46 };
    for (int i : arr) {
      System.out.println(i);
    }
  }
}

See the following output.

➜  java javac Loop.java
➜  java java Loop
18
19
21
29
46
➜  java

Java Infinitive For Loop

Using two semicolons (;;) in the for loop will be infinite for loop.

See the following syntax.

for(;;){  

// code to be executed  

}

See the following code example.

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

If you run the above code, it will go to infinite loop and print the AppDividend.

That’s it for the Java for loop tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.