Java For Loop

In Java, loops are used to execute a block of code repeatedly. One of the most commonly used loops is the for loop, which is used when we know the number of times we want to execute a code block. In this blog, we will discuss the for loop in detail and provide examples of its usage.

Syntax:

The syntax of a for loop in Java is as follows:

for (initialization; condition; increment/decrement) {
   // code block to be executed
}

Here’s what each part of the syntax means:

  • Initialization: This is where you initialize the loop variable. It is executed only once before the loop starts.
  • Condition: This is the condition that must be true for the loop to continue executing. If the condition is false, the loop will stop executing.
  • Increment/Decrement: This is where you update the value of the loop variable. It is executed after each iteration of the loop.

Example:

for (int i = 0; i < 10; i++) {
   System.out.println(i);
}

In the above example, we initialize the loop variable i to 0, set the condition to execute the loop until i is less than 10, and increment i by 1 after each iteration. The code block inside the loop simply prints the value of i.

Output: 0 1 2 3 4 5 6 7 8 9

Nested For Loops:

Java also supports nested for loops, where you can use one for loop inside another. This is useful when you need to iterate through a two-dimensional array or when you need to perform some operation on multiple lists.

Example:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < matrix.length; i++) {
   for (int j = 0; j < matrix[i].length; j++) {
      System.out.print(matrix[i][j] + " ");
   }
   System.out.println();
}

In the above example, we initialize a two-dimensional array, matrix, and use nested for loops to iterate through the array and print its values.

Output:

1 2 3 
4 5 6 
7 8 9 

Breaking and Continuing a For Loop:

Sometimes, you may need to break out of a for loop before it has completed all of its iterations. In such cases, you can use the break keyword to exit the loop.

Example:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      break;
   }
   System.out.println(i);
}

In the above example, the loop will exit when the value of i is 5. Therefore, the output will be:

Output: 0 1 2 3 4

You can also use the continue keyword to skip over certain iterations of the loop.

Example:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      continue;
   }
   System.out.println(i);
}

In the above example, the loop will skip over the iteration when i is 5 and continue with the next iteration. Therefore, the output will be:

Output: 0 1 2 3 4 6 7 8 9

In conclusion, the for loop is an essential tool for iterating through lists and arrays in Java. With the knowledge and examples provided in this blog, you can start using for loops in your Java programs to make them more efficient and flexible. The for loop allows you to control the number of times a block of code is executed and to perform operations on nested arrays and lists. Additionally, the break and continue keywords give you even more control over the execution of the loop.

We hope this blog has provided you with a good understanding of the for loop in Java and its usage. Feel free to use the examples provided in this blog to experiment with for loops in your own Java programs.