- Java for Loop
- Java for Loop
- Example 1: Display a Text Five Times
- Example 2: Display numbers from 1 to 5
- Example 3: Display Sum of n Natural Numbers
- Java Infinite for Loop
- Table of Contents
- Nested For Loop in Java | Pattern Example
- Syntax of Nested for loop in Java
- Flowchart Diagram for Nested Loops in Java
- Example Program based on Nested for loops
- Program based on Java Pattern using Nested for loop
- Alphabet Pattern using Nested for loops
- More Example Patterns for Best Practice
Java for Loop
In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop.
In Java, there are three types of loops.
This tutorial focuses on the for loop. You will learn about the other type of loops in the upcoming tutorials.
Java for Loop
Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is:
for (initialExpression; testExpression; updateExpression) < // body of the loop >
- The initialExpression initializes and/or declares variables and executes only once.
- The condition is evaluated. If the condition is true , the body of the for loop is executed.
- The updateExpression updates the value of initialExpression.
- The condition is evaluated again. The process continues until the condition is false .
To learn more about the conditions, visit Java relational and logical operators.
Example 1: Display a Text Five Times
// Program to print a text 5 times class Main < public static void main(String[] args) < int n = 5; // for loop for (int i = 1; i > >
Java is fun Java is fun Java is fun Java is fun Java is fun
Here is how this program works.
Iteration | Variable | Condition: i | Action |
---|---|---|---|
1st | i = 1 n = 5 | true | Java is fun is printed. i is increased to 2. |
2nd | i = 2 n = 5 | true | Java is fun is printed. i is increased to 3. |
3rd | i = 3 n = 5 | true | Java is fun is printed. i is increased to 4. |
4th | i = 4 n = 5 | true | Java is fun is printed. i is increased to 5. |
5th | i = 5 n = 5 | true | Java is fun is printed. i is increased to 6. |
6th | i = 6 n = 5 | false | The loop is terminated. |
Example 2: Display numbers from 1 to 5
// Program to print numbers from 1 to 5 class Main < public static void main(String[] args) < int n = 5; // for loop for (int i = 1; i > >
Here is how the program works.
Iteration | Variable | Condition: i | Action |
---|---|---|---|
1st | i = 1 n = 5 | true | 1 is printed. i is increased to 2. |
2nd | i = 2 n = 5 | true | 2 is printed. i is increased to 3. |
3rd | i = 3 n = 5 | true | 3 is printed. i is increased to 4. |
4th | i = 4 n = 5 | true | 4 is printed. i is increased to 5. |
5th | i = 5 n = 5 | true | 5 is printed. i is increased to 6. |
6th | i = 6 n = 5 | false | The loop is terminated. |
Example 3: Display Sum of n Natural Numbers
// Program to find the sum of natural numbers from 1 to 1000. class Main < public static void main(String[] args) < int sum = 0; int n = 1000; // for loop for (int i = 1; i System.out.println("Sum java-exec"> // Program to find the sum of natural numbers from 1 to 1000. class Main < public static void main(String[] args) < int sum = 0; int n = 1000; // for loop for (int i = n; i >= 1; --i) < // body inside for loop sum += i; // sum = sum + i >System.out.println("Sum /java-programming/arrays" title="Java arrays">arrays and collections. For example, // print array elements class Main < public static void main(String[] args) < // create an array int[] numbers = ; // iterating through the array for (int number: numbers) < System.out.println(number); >> >
Here, we have used the for-each loop to print each element of the numbers array one by one.
In the first iteration of the loop, number will be 3, number will be 7 in second iteration and so on.
Java Infinite for Loop
If we set the test expression in such a way that it never evaluates to false , the for loop will run forever. This is called infinite for loop. For example,
// Infinite for Loop class Infinite < public static void main(String[] args) < int sum = 0; for (int i = 1; i > >
Table of Contents
Nested For Loop in Java | Pattern Example
Nested for loop in Java means one for statement inside another for statement. In other words, a for loop nested inside another for loop is called nested for loops.
A nested for loops consists of an outer for loop and one or more inner for loops. Each time the outer for loop repeats, the inner for loop re-enters and starts a new execution.
That is, each time the control will enter inside the inner for loop when the outer for loop repeats.
We can put many loops inside a loop. But, it is advice that you do not go beyond three levels of nested loops, as it will make the program look clumsy.
Syntax of Nested for loop in Java
The general syntax for using nested for loop in Java is as follows:
// Outer for loop. for ( initialization; test-condition; increment ) < // Inner for loop. for ( initialization; test-condition; increment ) < // statement of inner loop >// statement of outer loop >
Let’s understand it with the help of an example.
for(int i = 1; i for(int j = 1; j
If we write first for loop inside the second for loop, it will look like this:
a) In this example, when i = 1, execution will start from the outer for loop and statement1 will execute once.
b) Now, the control of execution enters inside the inner for loop. Since we initially set the control variable j to 1, statement2 will execute once.
c) After this execution, the value of j will be 2 because of increment by 1. Then, statement2 will execute once again.
d) Like this, the inner for loop will execute 4 times with changing j values from 1 to 4. This means that statement2 will execute 4 times in the first execution of outer for loop.
e) When the execution of inner for loop completes, the control of execution goes to the outer for loop. Now, the value of i will be 2 because of increment by 1.
This time, the control of execution again enters inside the inner for loop and statement2 will execute 4 times.
f) Then, the control again goes to outer for loop and the value of i will set to 3. Again, the inner for loop will execute 4 times.
This means that the values of i and j will change as:
In the above sequence, the outer for loop will execute total 3 times. Hence, statement1 inside the loop body will also execute 3 times.
Since the inner for loop will execute 4 times for each i value therefore, statements2 inside the loop body will execute 12 times.
In the same way, we can also nest a while loop or a do-while loop inside a for loop and vice versa. These are called nested loops in Java.
Flowchart Diagram for Nested Loops in Java
The basic flowchart diagram for nested loops in Java has shown in the below figure.
Thus, we can also nest a while loop or a do-while loop inside a for loop and vice versa. These are called nested loops in Java.
The general syntax to nest while loop inside a for loop is as:
for(initialization; test-condition; increment/decrement) < statements; while(conditional expression) < statements; >>
Example Program based on Nested for loops
Let’s write a program in which we will display values of the inner for loop for each outer iteration, as well as outer for loop.
Program code 1:
public class NestedForLoopEx < public static void main(String[] args) < // Outer for loop. for(int i = 1; i System.out.println(" "); > > >
Output: 1 1 2 3 4 2 1 2 3 4 3 1 2 3 4
As you observe in the output, during each iteration of outer for loop, the statement inside the inner for loop executes 4 times. Thus, outer for loop executes three times and inner for loop executes total four times.
2. Let’s write Java program to display tables for 1 and 2 using nested for loops.
Program code 2:
public class Tables < public static void main(String[] args) < System.out.println("Displaying Tables: "); // Outer for loop. for(int i = 1; i System.out.println(" "); > > >
Output: Displaying Tables: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
Program based on Java Pattern using Nested for loop
1. Let’s write Java program to display triangle of * using nested for loop.
Program code 3:
public class PatternEx < public static void main(String[] args) < System.out.println("Displaying right triangle of *: "); // Outer for loop. for(int i = 1; i System.out.println(" "); > > >
Output: Displaying right triangle of *: * * * * * * * * * * * * * * *
2. Let’s create a Java program to display a triangle of numbers.
Program code 4:
public class PatternEx < public static void main(String[] args) < System.out.println("Displaying right triangle pattern of numbers: "); // Outer for loop. for(int i = 1; i System.out.println(" "); > > >
Output: Displaying right triangle pattern of numbers: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Program code 5:
public class PatternEx < public static void main(String[] args) < int k = 1; System.out.println("Displaying right triangle pattern of numbers: "); // Outer for loop. for(int i = 1; i System.out.println(" "); k++; > > >
Output: Displaying right triangle pattern of numbers: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
3. Let’s create a Java program to the display the following pattern using nested for loops.
Program code 6:
public class PatternEx < public static void main(String[] args) < System.out.println("Displaying pattern of numbers: "); // Outer for loop. for(int i = 5; i >= 1; i--) < // Inner for loop. for(int j = 1; j System.out.println(" "); > > >
Output: Displaying pattern of numbers: 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Alphabet Pattern using Nested for loops
4. Let’s write program code to print the following alphabet pattern using Java nested for loops.
Program code 7:
public class PatternEx < public static void main(String[] args) < System.out.println("Displaying alphabet pattern: "); // Outer for loop. for(int i = 65; i System.out.println(" "); > > >
Output: Displaying alphabet pattern: A A B A B C A B C D A B C D E
Program code 8:
public class PatternEx < public static void main(String[] args) < System.out.println("Displaying alphabet pattern: "); // Outer for loop. for(int i = 69; i >= 65; i--) < // Inner for loop. for(int j = 65; j System.out.println(" "); > > >
Output: Displaying alphabet pattern: A B C D E A B C D A B C A B A
More Example Patterns for Best Practice
Program code 9:
public class PatternEx < public static void main(String[] args) < int k = 65; System.out.println("Displaying alphabet pattern: "); // Outer for loop. for(int i = 65; i = 65; j--) < if(j >i) System.out.print(" "); else System.out.format("%c ", k++); > System.out.println(" "); > > >
Output: Displaying alphabet pattern: A B C D E F G H I
Program code 10:
public class PatternEx < public static void main(String[] args) < int i, j, k = 1; for(i = 1; i = 1; j--) < if(j >i) System.out.print(" "); else System.out.print(k++ +" "); > System.out.println(); > > >
In this tutorial, you learned nested for loop in Java with various example programs. Hope that you will have understood the basic points of nested for loop and practiced the following patterns.
Thanks for reading.