• Java Video Tutorials

Java - for Loop


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.

A for loop is useful when you know how many times a task is to be repeated.

Syntax

The syntax of a for loop is −

for(initialization; Boolean_expression; update) {
   // Statements
}

Here is the flow of control in a for loop −

  • The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables and this step ends with a semi colon (;).

  • Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop will not be executed and control jumps to the next statement past the for loop.

  • After the body of the for loop gets executed, the control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank with a semicolon at the end.

  • The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

Flow Diagram

Java For Loop

Example 1

In this example, we're showing the use of a for loop to print numbers starting from 10 to 19. Here we've initialized an int variable x with a value of 10 within initialization blook of for loop. Then in expression block, we're checking x as less than 20, and in the end under update block, we're incrementing x by 1. Within body of for loop, we're printing the value of x. For loop will run until x becomes 20. Once x is 20, loop will stop execution and program exits.

public class Test {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x = x + 1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

Output

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

Example 2

In this example, we're showing the use of a for loop to print contents of an array. Here we're creating an array of integers as numbers and initialized it some values. We've created a variable named index to represent index of the array within for loop, check it against size of the array and incremented it by 1. Within for loop body, we're printing element of the array using index notation. Once index becomes same as array size, for loop exits and program quits.

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int index = 0; index < numbers.length; index++) {
         System.out.print("value of item : " + numbers[index] );
         System.out.print("\n");
      }
   }
}

Output

value of item : 10
value of item : 20
value of item : 30
value of item : 40
value of item : 50

Example 3

In this example, we're showing the infinite loop using for loop. It will keep printing the numbers until you press ctrl+c to terminate the program.

public class Test {

   public static void main(String args[]) {
      int x = 10;

      for( ;; ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

Output

value of item : 10
value of item : 11
value of item : 12
value of item : 13
value of item : 14
...
ctrl+c
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements