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.
See online demo and code
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class test1 { public static void main(String []args) { int x; x=0; for (x=0; x<10; x++){ System.out.println("The value of x in this iteration= " + x); } } } |
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
See online demo and code
The Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class test1 { public static void main(String []args) { int x; for (x=100; x>1; x=x-10){ System.out.println("x = " + x); } } } |
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:
See online demo and code
The Java code using for loop with break statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class test1 { public static void main(String []args) { int x; for (x=0; x<100; x=x+10){ System.out.println("x = " + x); if (x == 50) { System.out.println("The Loop is broken"); break; } } } } |
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.
See online demo and code
The nested for loop code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class test1 { public static void main(String []args) { int x,y,z; for (x=0; x<50; x=x+10){ for (z=1; z<3; z++){ y = x + z; System.out.println(x + "+" + z + " = " + y); } } } } |
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:
See online demo and code
The Java code with array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class test1 { public static void main(String []args) { int[] arrNums = {10,20,30,40,50}; for (int currElement : arrNums) { System.out.println("The array item: " + currElement); } } } |
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:
See online demo and code
The Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class test1 { public static void main(String []args) { String[] arrstr = new String[3]; arrstr[0] = "Houston"; arrstr[1] = "Chicago"; arrstr[2] = "Dallas"; for (String currElement : arrstr) { System.out.println("The US city: " + currElement); } } } |
You can see, how enhanced for loop makes it easier to iterate through array elements.