for loop in Java
-
Last Updated: October 20, 2023
-
By: javahandson
-
Series
In this article, we will learn what is a for loop in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
We will use the for loop when we have to execute the set of statements a fixed number of times. The for loop executes a set of statements as long as the condition is true.
for loop is almost the same as do…while or while loop but syntactically for loop is more concise and easy to debug because the initialization, condition and increment/decrement counter are present in one line.
for( expression1; expression2; expression3) { // execute a set of statements }
We will try to understand it better with an example
public class Test { public static void main(String[] args) { for (int i=1; i<=5; i++) { System.out.println(i); } } } Output : 1 2 3 4 5
a. We are initializing the variable i = 1.
b. We will check the condition if i <= 5 since 1 <= 5 the condition is true hence we will execute the statements inside the for loop and we will print the value of i i.e. 1.
c. Now expression3 gets triggered here we are incrementing the value of i. Now the value of i becomes 2.
d. Second expression is again executed i.e. i <= 5 since 2 <= 5 the condition is true hence we will execute the statements inside the for loop and we will print the value of i i.e. 2.
e. expression3 gets triggered again and the steps are repeated till the value of i becomes 6. Once i = 6 the condition is false and we come out of the loop.
Note* expression1 is executed only once at the beginning of the for loop whereas expression2 and expression3 will be repeated multiple times as long as the expression2 is true.
We can write a for loop without expression1 i.e. without initialization expression and still, the loop works properly.
We have to write the initialization expression before the for loop.
int i =1 is the initialization expression in the below example.
public class Test { public static void main(String[] args) { int i=1; for ( ; i<=5; i++) { System.out.println(i); } } } Output : 1 2 3 4 5
We can write a for loop without expression1 and expression2 still the loop works properly.
We have to specify the conditional expression somewhere inside the for loop otherwise, the loop will run infinitely and we will get an error.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { int i=1; for ( ; ; i++) { System.out.println(i); if (i >= 5) { break; } } } } Output : 1 2 3 4 5
In the above example as soon as i becomes 5 break statement will be executed and we will come out of the for loop terminating the loop. Here i >= 5 is the conditional expression.
We can write a for loop without expression1, expression2 and expression3 still the loop works properly.
We have to specify the conditional expression somewhere inside the for loop otherwise, the loop will run infinitely and we will get an error.
We also have to specify the variable value modification expression inside the for loop otherwise the variable value will remain constant and the condition will be always true hence the loop will run infinitely throwing an error.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { int i=1; for ( ; ; ) { System.out.println(i); if (i >= 5) { break; } i++; } } } Output : 1 2 3 4 5
In the above example as soon as i becomes 5 break statement will be executed and we will come out of the for loop terminating the loop. Here i >= 5 is the conditional expression and i++ is the variable value modification expression.
In the do…while loop or while loop we will not know how many times we have to execute the loop. It just depends on the condition. Whereas in the case of a for loop we will know how many times we have to execute the loop. We will try to understand it better with an example
Ex. Write a program to satisfy the below points
a. Initialize a variable counter as 1.
b. Increment the counter by 3.
c. Print the value of the counter.
d. Stop printing once the counter reaches 22.
package com.java.handson.control.statements; public class Test { public static void main(String[] args) { boolean printValue = true; int counter = 1; while (printValue) { counter = counter + 3; System.out.print(counter + " "); if (counter == 22) printValue = false; } } } Output : 4 7 10 13 16 19 22
In the above example, we don’t know how many times we have to execute the loop hence we have used a while loop.
So this is all about the for loop in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.