For Loops in Java

In this tutorial we are going to learn about for loops in Java with the help of some handy examples that will tell us more about how loops work in programming.


What are For Loops in Java

Generally when we do programming, we need a certain kind of condition block to repeat itself until we get our desired solution, for that we need for loops. For example,

Let’s say we want to execute hello world on our computer screen for 10 times, we can do it by using print line statements multiple times without using loops. but what will you do when you want to print the hello world statement for a million times, for that you need for loops.

Let’s look at the syntax of for loops in Java:

Syntax of For Loop in Java

for (declaration, condition, update) {
// code of block

}

Let’s classify further how the for loop works.

Recommended Books



How the For Loop Works

For Loops in Python

  • The declaration expression inside the for loop statement runs only once.
  • Then the for loop reviews the condition expression which is second in a row, once the condition is evaluated and turns out to be true, the code block gets executed.
  • The for loop then goes on to the third expression which is the update expression. If the condition is true then the update expression also gets executed.
  • This process repeats itself as long as the condition expression is true.
  • Once the condition expression turns out to be false, the for loop ends.

Example:

public class for_loop {
  public static void main(String[] args) {

    for (int x = 0; x < 10; x++) {
      System.out.println("Hello World");
    }
  }
}

Output:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

In the above example we can see that we have initialised x variable and set it equal to zero. Next, we have applied a condition of x should be less than 10 followed by the update expression x++.

Inside the code block we are executing the print statement with the hello world string. The for loop continues to run until x becomes 10. When x becomes 10 then the condition expression x < 10 becomes false and therefore the for loop ends.





Let’s look at another example that calculates the sum of of x variable ranging from 1 to 100.

Example:

public class sum_calc {
  public static void main(String args[]) {
    int sum_of = 0;

    for (int x = 1; x <= 100; x++) {
      sum_of = sum_of + x;
    }
    System.out.println("Sum total is: " + sum_of);
  }
}

Output

Sum total is: 5050

Infinite For Loop

You can also create an infinite for loop, but that is never a good idea because inside an infinite for loop, the condition would never turn out false. Therefore, the for loop will run infinitely and your computer may crash. For example:

public class Infinite {
  public static void main(String[] args) {

    for (int x = 1; x <= 10; --x) {
      System.out.println("Infinite");
    }
  }
}

Output:

Infinite
Infinite
Infinite
…

As you can see above, that there is never gonna be a false expression, hence, infinite is printed over and over again.