codingstreets
Search
Close this search box.

Get Started: Java For Loop

In This Article, You Will Learn about Java For Loop.

Java For Loop – Before moving ahead, let’s know a bit about Java while Loop.

Table of Contents

For Loop

For Loop is another loop in Java like a while loop. For Loop is used when we have to iterate over a particular time through a list or group of items and a code block.

Syntax

				
					for (statement 1; statement 2; statement 3) {
  // code block to be executed
}
				
			

Explanation:

Statement 1 – It executes only one time before the execution of the code block.

Statement 2 – It defines the condition to execute the code block.

Statement 3 – It executes every time after the condition’s code block is executed.

Example: Write the number from 10 to 5 by using for loop.

				
					public class Main {
  public static void main(String[] args) {
    for (int i = 10; i < 5; i - -) {
      System.out.println(i);
    }  
  }
}
				
			

In the above example, as per the condition, in each iteration number 1 will be subtracted (i – -) from the current value of i, and this process will keep going on until i = 6.

Step 1: Since i = 10, it is greater than 5 according to the condition defined. Later on, 1 number will be subtracted as per the condition i – – and i = 9.

Above the same condition will keep going on until i = 6 and to the next step when i = 6 will be checked for i < 5, it will be false; therefore loop will break and stop the execution.

Example: Create an Infinite loop with for loop.

				
					public class Main {
  public static void main(String[] args) {
    for (int i = 5; i < 10; i--) {
      System.out.println(i);
    }  
  }
}
				
			

In the above example, as per the condition, in each iteration number 1 will be  (i – -) from the current value of i and this process will keep going on forever because i = 5 will always be less than i = 10; therefore condition will always be true and the loop will keep continuing the execution the code block.

Example: Write the number from 15 to 5 by using for loop. At intervals of 2 numbers.

				
					public class Main {
  public static void main(String[] args) {
    for (int i = 15; i > 5; i = i - 2) {
      System.out.println(i);
    }  
  }
}
				
			

In the above example, as per the condition, in each iteration number 1 will be added (i = i + 2) from the current value of i, and this process will keep going on until i = 5.

Step 1: Since i = 15, it is greater than 5 according to the condition defined. Later on, number 2 will be subtracted as per the condition, and become i = 5.

Above the same condition will keep going on until i = 7 and to the next step when i = 5 will be checked for i > 5, it will be false; therefore loop will break and stop the execution.

Example: Write the number from 10 to 5 by using for loop.

				
					public class Main {
  public static void main(String[] args) {
    for (int i = 20; i > 11; i = i+2) {
      System.out.println(i);
    }  
  }
}
				
			

In the above example, as per the condition, in each iteration number 2 will address (i = i+2) from the current value of i, and this process will keep going on forever because i = 11 will always be less than i = 20; therefore condition will always be true and the loop will keep continuing the execution the code block.

For-Each Loop

There is also a “for-each” loop, which is used to loop through elements in an array.

Syntax

				
					for (type variableName : arrayName) {
  // code block to be executed
}
				
			

Example: Print the list of student’s name by using for-each loop.

				
					public class Main {
  public static void main(String[] args) {
    String[] names = {"AK", "JK", "PK", "SK"};
    for (String i : names) {
      System.out.println(i);
    }    
  }
}
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on