For Loop in Java is one of the basic and most used looping statement. It executes a block of statements repeatedly until the condition specified becomes false. In this article we will learn end to end on how to use for loop in Java.
1. Syntax
for (Initialization; BooleanExpression; Increment/Decrement) { Body of Loop //executed when the condition is satisfied }
- Initialization : This will be executed only one time, during the start of the loop
- BooleanExpression : The BooleanExpression will be executed every time when the loop iterates. This block will be executed until the condition returns false.
- Increment/Decrement : This part will be executed each time at the end of each iteration.
2. Flow Diagram
3. For Loop in Java Example
package com.javainterviewpoint; public class ForLoop_Example { public static void main(String args[]) { for(int i=1;i<=10;i++) { System.out.println("Value of i is : "+i); } } }
While executing the above code the following happens
- Initially the value of i will be set to 1(Occurs only one time).
- Condition (i<=10), will be executed everytime until i value is greater than 10.
- Increment (i++) occurs at the end of each iteration(i.e) after printing the value of i
Important point to be noted here is each part is separated by a semi-colon(;)
Output
Value of i is : 1 Value of i is : 2 Value of i is : 3 Value of i is : 4 Value of i is : 5 Value of i is : 6 Value of i is : 7 Value of i is : 8 Value of i is : 9 Value of i is : 10
4. Usage of Comma
There will be times where you need to work with two variables, both needs to be initialized and incremented/decremented as in the below code.
package com.javainterviewpoint; public class ForLoop_Example { public static void main(String args[]) { int j=10; for(int i=1;i<=10;i++) { System.out.println("Value of i is : "+i); System.out.println("Value of j is : "+j); j--; } } }
Output
Value of i is : 1 Value of j is : 10 Value of i is : 2 Value of j is : 9 Value of i is : 3 Value of j is : 8 Value of i is : 4 Value of j is : 7 Value of i is : 5 Value of j is : 6 Value of i is : 6 Value of j is : 5 Value of i is : 7 Value of j is : 4 Value of i is : 8 Value of j is : 3 Value of i is : 9 Value of j is : 2 Value of i is : 10 Value of j is : 1
Here in the above code you can see that the loop is governed by two variables i and j, it would be convenient if both are handled by loop itself rather than handling j alone manually. Now comma operator comes to the rescue it lets you handle more than one variable in initialization and increment. Now the code can be efficiently re-written like below.
package com.javainterviewpoint; public class ForLoop_Example { public static void main(String args[]) { for(int i=1,j=10;i<=10;i++,j--) { System.out.println("Value of i is : "+i); System.out.println("Value of j is : "+j); } } }
5. Different ways of looping ‘For’
Increment inside body
public class ForLoop_Example
{
public static void main(String args[])
{
for(int i = 1;i<=10;)
{
System.out.println("Value of i is : "+i);
i++;
}
}
}
Initialization outside & Increment inside body
public class ForLoop_Example { public static void main(String args[]) { int i = 1; for(;i<=10;) { System.out.println("Value of i is : "+i); i++; } } }
Empty Loop
public class ForLoop_Example { public static void main(String args[]) { int i = 1; for(;;) { System.out.println("Value of i is : "+i); if(i>=10) { break; } i++; } } }
Leave a Reply