With the help of examples, we will learn how to utilize the for loop in Java in this lesson. We will also understand how the loop functions in computer programming.
Loops are used in computer programming to repeatedly run a block of code. Instead of inputting the same code 100 times, you might use a loop, for instance, to display the same message 100 times.
There are three different forms of loops in Java.
- for loop
- while loop
- do…while loop
The for loop is the subject of this tutorial.
In the upcoming tutorials, you will learn about the other types of loops.
Java for Loop
To run a block of code a predetermined number of times, use a Java for loop. The for loop’s syntax is:
for (initialExpression; testExpression; updateExpression) {
// body of the loop
}
Here,
- The initial expression only executes once and initializes or declares variables.
2. A condition assessment is done. The for loop’s body is executed if the condition is satisfied.
3. The initial expressions value is updated by the update expression.
4. Another assessment of the situation. Up until the condition is false, the process keeps going.
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 <= n; ++i) {
System.out.println("Java is fun");
}
}
}
Output
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
Here is how this program works.
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 <= n; ++i) {
System.out.println(i);
}
}
}
Output
1
2
3
4
5
Here is how the program works.
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 <= n; ++i) {
// body inside for loop
sum += i; // sum = sum + i
}
System.out.println("Sum = " + sum);
}
}
Output
sum = 500500
In this case, the sum starts out with a value of 0. The for loop is then executed from I = 1 to 1000 times. Is added to the sum and its value is raised by 1 with each iteration.
The test condition is false when I reach 1001, and the sum will be equal to 0 + 1 + 2 +…. + 1000.
The program mentioned above that adds up all natural numbers can also be expressed as
// 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 = " + sum);
}
}
This program’s output matches that of Example 3 exactly.
Java for-each Loop
// print array elements
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {3, 7, 5, -5};
// iterating through the array
for (int number: numbers) {
System.out.println(number);
}
}
}
Output
3
7
5
-5
Here, the for-each loop is being used to print the numbers array’s elements one by one.
The loop’s initial iteration will involve the number 3, the second iteration will involve the number 7, and so on.
Java Infinite for Loop
// Infinite for Loop
class Infinite {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; --i) {
System.out.println("Hello");
}
}
}
Here, Hello is repeatedly printed until the memory runs out while the test expression, I = 10, is never false.