AppDividend
Latest Code Tutorials

Java For Loop Example | For Loop in Java Tutorial

0

Java Loops are used to 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 following three types of loops in java.

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

In this article, we will see the For loop in Java with example.

Java For Loop

If a number of iteration is fixed, it is recommended to use the 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.

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 the execution of 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 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 simpler way to iterate through the items of a collection or array. It is inflexible and should only be used when there is a requirement to iterate through the items in a sequential manner without knowing an index of the currently processed item.

Also, note that an object/variable is an 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 modified.

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

The for-each loop is used to traverse 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 on items basis 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

If you use two semicolons (;;) in the for loop, it 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, then it will go to infinite loop and print the AppDividend.

Finally, Java For Loop Example | For Loop in Java Tutorial is over.

Leave A Reply

Your email address will not be published.

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