6 variety of examples of using Java for loop

The for loop of Java

The Java for loop is used to iterate through the block of code over the range of values.

The Java provides simple as well as “enhanced” for statement that is used with arrays and other collections. I will show you using both ways in this tutorial.

The simple for loop in Java (basically, the for statement) can be used as follows.

 

Structure of for loop

for (initialization_expression; termination_expression; increment_decrement_expression) {

Code to be executed here

}

The definition of each expression is given below:

  • When the for loop begins, the initialization expression initiates the loop. Also, this part of the loop executes only once.
  • The second expression is termination expression. As long as the termination expression evaluates as true, the loop keeps on executing. When it becomes false, the loop ends.
  • The third one is an increment or decrement operator. This expression is invoked after each iteration and so the value is increased or decreased.

The following section explains how you may use the for Java loop with demos and code.

A demo of using for loop to display numbers

In this example, the for loop is used for displaying the numbers from 0 to 9. For that, the variable is assigned the value 0 as an initial expression. The condition is to iterate the loop until the value of variable x is less than 10. In each iteration, the value of x is incremented by 1.

Java for loop

See online demo and code

The code:

 

A demo of for loop with decrement operator

In this demo, the value of variable x is decremented by 10 in each iteration. The initial value is set as 100. The third expression is used as follows:

x=x-10

for loop decrement

See online demo and code

The Java code:

 

You see, this is how the for loop is built:

    for (x=100; x>1; x=x-10){

Inside the curly braces, the system.out.println statement is used to display the value of x variable.

A demo of using the break statement in for loop

In this example of explaining the for loop of Java with the break statement, I initiated the variable x = 0. In each iteration, the value of x is incremented by 10. The iteration should go on until the value of x is greater than 100. However, inside the curly braces, after using the system.out.println for displaying the value of x, the if statement is used to check the value of x variable. If the value is equal to 50, the loop should break; have a look:

for loop break

See online demo and code

The Java code using for loop with break statement:

 

A demo of using nested for loop

You can use a for loop inside another for loop as writing the Java programs. This is called nested for loop. The nested for loop may be required if you need to run another loop for each item of the outer array.

In the following demo, an outer loop is used where the x variable is initiated from 0 and will loop until the value reaches 50. In each iteration, the value is incremented by 10.

In the inner loop, the variable z is initiated from 1 and increments by 1 in each iteration till the variable z value is less than 3.

Inside the curly braces of the inner loop, the value of x and z is summed and assigned to the variable y. Finally, the System.out.println is used to display the current value of x, z and y.

for loop nested

See online demo and code

The nested for loop code:

 

An example of enhanced for loop with array

As mentioned earlier, you may use the for loop to iterate through the arrays or other collections in Java. In this example, a numeric array is created with five elements. After that, the “enhanced” for loop is used to iterate through that array and System.out.println is used to display the array items:

for loop array

See online demo and code

The Java code with array:

 

You see, the currElement variable is used in the for loop which is followed by the array name. In each iteration, the next element of the array is assigned to that variable which is displayed by using the System.out.println.

A demo of for loop with a string array

Similarly, you may use the string array with for loop to iterate through its elements. See this example, where an array with three elements is created. By using the for loop, the array items are displayed:

for loop array string

See online demo and code

The Java code:

 

You can see, how enhanced for loop makes it easier to iterate through array elements.