Prerequisite: Loops in Java
The structure of basic for loop:
for(initialization; boolean expression; update statement)
{
//Body
}
Let’s look at some basic examples of using for loop and the common pitfalls in using for loop
- Providing expression in for loop is must : For loop must consist a valid expression in the loop statement failing which can lead to an infinite loop. The statement
for ( ; ; ) is similar to while(true)
// Java program to illustrate// infinite looppublicclassExample1{publicstaticvoidmain(String[] args){for( ; ; ){System.out.println("This is an infinite loop");}}}chevron_rightfilter_noneOutput: This code prints the statement “This is an infinite loop” repeatedly.
- Initializing multiple variables : In Java, multiple variables can be initialized in initialization block of for loop regardless of whether you use it in the loop or not.
// Java program to illustrate// Initializing multiple variables// in initialization blockpublicclassExample2{publicstaticvoidmain(String[] args){intx =2;for(longy =0, z =4; x <10&& y <10; x++, y++){System.out.println(y +" ");}System.out.println(x);}}chevron_rightfilter_noneIn the above code, there is simple variation in the for loop. Two variables are declared and initialized in the initialization block. The variable ‘z’ is not being used. Also, the other two components contain extra variable. So, it can be seen that the blocks may include extra variables which may not be referenced by each other.
- Redeclaration of a variable in initialization block : Suppose, an initialization variable is already declared as integer. Can we re-declare it in for loop with other data type? No, See the example:
// Java program to illustrate// redeclaring a variable// in initialization blockpublicclassExample3{publicstaticvoidmain(String[] args){// x is integerintx =0;// redeclaring x as long will not workfor(longy =0, x =1; x <5; x++){System.out.print(x +" ");}}}chevron_rightfilter_noneExample3.java:12: error: variable x is already defined in method main(String[]) for(long y = 0, x = 1; x < 5; x++)Here, x was already initialized to zero as integer and is being re-declared in the loop with data type long.
But this problem can be fixed by slightly modifying the code. Here, the variables x and y are declared in a
different way.
// Java program to illustrate// redeclaring a variable// in initialization blockpublicclassExample3{publicstaticvoidmain(String[] args){// x is integerintx =0;longy =10;for(y =0, x =1; x <5; x++){System.out.print(x +" ");}}}chevron_rightfilter_noneOutput:
1 2 3 4
- Variables declared in the initialization block must be of same type : It is just a common sense that when we declare a variable as
int x, y;
both variables are of same type. Its just the same in for loop initialization block too.
// Java program to illustrate// declaring a variable// in initialization blockpublicclassExample4{publicstaticvoidmain(String[] args){// This will cause error;// int x;// redeclaring x as long will not workfor(longy =0, x =1; x <5; x++){System.out.print(x +" ");}}}chevron_rightfilter_none - Variables in the loop are accessible only within: The variables that are declared in the initialization block can be accessed only within the loop. For more on scope of variables, Refer here
// Java program to illustrate// scope of Initializing variables// within the looppublicclassExample5{publicstaticvoidmain(String[] args){// x and y scope is only// within for loopfor(intx =0, y =0; x <3&& y <3; x++, y++){System.out.println(y +" ");}System.out.println(x);}}chevron_rightfilter_noneError
Example5.java:13: error: cannot find symbol System.out.println(x);In the above example, variable x is not accessible outside the loop. The statement which is commented gives compiler error.
This article is contributed by Preeti Pardeshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

