For Loop Java Tutorial

Introduction

Guide to For Loop Java – Loops are used to execute a series of statements repeatedly until a certain condition is met. There are three sorts of fundamental loops in Java: for, while, and do-while. In this tutorial, we will learn how to use Java for loop.

Syntax:

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

Flow of Execution

The interpreter always maintains track of which statement is scheduled to be executed when a program runs. This is referred to as the control flow or the flow of execution of a program.

For Loop Flow

Steps:

  • Step 01: In the for loop, initialization occurs first and only once, which implies that the for loop’s initialization section only executes once.
  • Step 02: On each iteration, the condition in the for loop is tested; if the condition is true, the statements in the for loop body are perform. When the condition returns false, the lines in the for loop are not execute. And control is move to the statement after the for loop in the program.
  • Step 03: The increment/decrement component of the for loop occurs after each execution of the for loop’s body, which changes the loop counter.
  • Step 04: Following the third step, the control returns to the second step and the condition is re-evaluated.

Coding Examples

Simple For Loop Java Example

Code 01:

class Main {
    public static void main(String args[]){
         for(int i=1; i<10; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}

Output:

The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9

Infinite For Loop Java Example

Code 02:

class Main {
    public static void main(String args[]){
         for(int i=1; i>=1; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}

Because the condition would never return false, this is an infinite loop. Because we are incrementing the value of “i” it will always be larger than 1 (the Boolean expression: i>1), and hence will never return false. This would eventually result in an unending cycle. Thus, the coordination between the Boolean expression and the increment/decrement action is critical in determining. Whether or not the loop will end at some point in time.

For loop Java Example to Iterate an Array

Using the for loop, we iterate through array elements and display them.

Code 03:

class Main {
    public static void main(String args[]){
         int arr[]={1,2,3,4,5};
         //i starts with 0 as array index starts with 0 too
         for(int i=0; i<arr.length; i++){
              System.out.println(arr[i]);
         }
    }
}

Output:

1
2
3
4
5

Enhanced For Loop Java

The Enhanced for loop is useful for iterating Arrays/Collections since it is simple to design and understand. Take the previous example and rebuild it using an enhanced for loop.

Code 04:

class Main {
   public static void main(String args[]){
      int arr[]={1,2,3,4,5};
      for (int num : arr) {
         System.out.println(num);
      }
   }
}

Output:

1
2
3
4
5

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How To Call a Method in Java – [With Examples]

Leave a Comment