Java - For Loop

Java

In this tutorial we will learn about for loop in Java programming language.

The for loop is a more compact version of the while and do-while loop.

For syntax

for (initialise; condition; update) {
  // body of the loop
}

Where, initialise is a part were we initialise conditional variables. And this portion is executed only once.

The condition is a condition that decides whether the body of the loop will be executed or not. The condition is checked before every execution of the body of the loop.

The update portion is were we update the conditional variables. This portion is executed after executing the body of the loop.

Example #1: Write a program in Java to print "Hello World" five times using for loop

In the following example we will use counter in the initialise portion of the for loop.

In the condition part of the for loop we will use the following condition counter <= 5. So, as long as counter is less than or equal to 5 we will execute the body of the for loop.

In the update portion we will increment the value of counter by 1 using the increment operator.

class LoopExample {
  public static void main(String args[]) {
    for (int counter = 1; counter <= 5; counter++) {
      System.out.println("Hello World");
    }
  }
}

Output:

Hello World
Hello World
Hello World
Hello World
Hello World

Example #2: Write a program in Java to print from 1 to 10 but exit the loop if multiple of 7 is encountered

To find the multiple of 7 we will use the Modulus operator. We will divide the number by 7 and if the remainder is zero then the number is a multiple of 7.

To jump out of the loop we are using the break statement.

class LoopExample {
  public static void main(String args[]) {
    for (int counter = 1; counter <= 10; counter++) {
      System.out.println(counter);

      if (counter % 7 == 0) {
        System.out.println("Multiple of 7 encountered. Quitting loop.");
        break;
      }
    }
  }
}

Output:

1
2
3
4
5
6
7
Multiple of 7 encountered. Quitting loop.

Nesting for loops

We can nest a for loop inside another for loop as follows.

for (initialise_outer; condition_outer; update_outer) {

  // body of the outer for loop

  for (initilise_inner; condition_inner; update_inner) {
    // body of the inner for loop
  }
}

Example #3: Write a program in Java to print the following pattern

*
* *
* * *
* * * *

To solve this we can use two for loops. The first for loop is for the number of rows. The second for loop is for the number of * in the given row. </>

class LoopExample {
  public static void main(String args[]) {
    for (int r = 0; r < 4; r++) {
      for (int c = 0; c <= r; c++) {
        System.out.print("* ");
      }
      // after completing the inner loop
      // go to next line
      System.out.println();
    }
  }
}

Output:

* 
* * 
* * * 
* * * *

Note! print() method prints the output and keeps the cursor at the same line.

Whereas, println() method prints the output and takes the cursor to the next line.

For each loop

This is similar to a for loop but has a different syntax.

for (dataType varName: arrayVar) {
  // some code...
}

Where, dataType is the data type of the variable varName and same as the data type of the array arrayVar whose content we want to check out.

Example #4: Write a program in Java to print the content of an integer array using the for each loop

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

    // array
    int[] num = {1, 1, 2, 3, 5, 8, 13, 21};

    for (int i: num) {
      System.out.println(i);
    }
  }
}

Output:

1
1
2
3
5
8
13
21