Java For loop
Understanding for loop in Java
Key Take aways :
- For loop consist of three parts initialization, condition check and expression evaluation section.
- During the first run of for loop initialization and condition check happens and then for loop block gets executed.
- After the first run for all subsequent run the expression Evaluation(increment, decrement ) gets evaluated and then condition check happens and then code block gets executed.
- The condition check only accepts boolean Type as argument. No other type is acceptable (No int , Integer, long value).
- The condition check looks for boolean Type of a expression which results into Boolean.
Lets go through examples.
- For Loop with int i declared in initialization section and visible only in for block
public void loop1() {
// For loop consists of three part
// Initialization , conditional check , expression evaluation (increment decrement)
for (int i = 0; i <= 3; i++) {
System.out.println("value of i inside LOOP1 method is : " + i);
}
// System.out.println( i); error. i can be accessed only within the for loop
}
value of i inside LOOP1 method is : 1
value of i inside LOOP1 method is : 2
value of i inside LOOP1 method is : 3
[addToAppearHere]
- For Loop with int i declared outside of initialization section and visible only in for block
public void loop2() {
// For loop consists of three part
// Initialization , conditional check (boolean) , expression evaluation (increment decrement)
int i = 0;
for (; i <= 3; i++) {
System.out.println("value of i inside LOOP2 method is : " + i);
}
System.out.println("value of i in Looop2 outside for loop is " + i);
}
value of i inside LOOP2 method is : 0
value of i inside LOOP2 method is : 1
value of i inside LOOP2 method is : 2
value of i inside LOOP2 method is : 3
value of i in Looop2 outside for loop is 4
- Infinite Loop in For loop
public void loop3() {
for (; true; ) {
// infinite for loop in Java
}
// for(;1;) 1 is not taken as true. For accepts only boolean condition
}
- condition check parameter supplied from a method
private int getConditionSize() {
System.out.println("getConditionSize is being called");
return 3;
}
public void loop4() {
for (int i = 0; i <= getConditionSize(); i++) {
System.out.println("value of i inside LOOP4 method is : " + i);
}
}
getConditionSize is being called
value of i inside LOOP4 method is : 0
getConditionSize is being called
value of i inside LOOP4 method is : 1
getConditionSize is being called
value of i inside LOOP4 method is : 2
getConditionSize is being called
value of i inside LOOP4 method is : 3
getConditionSize is being called
[addToAppearHere]
- Iterating over a iterator using for loop
public void loop5() {
List names = Arrays.asList("Maverick", "Rock");
for (String name : names) {
System.out.println("In LOOP5 name is " + name);
}
for (int i = 0; i < names.size(); i++) {
System.out.println("In LOOP5 iterating through traditional way is " + names.get(i));
}
}
In LOOP5 name is Maverick
In LOOP5 name is Rock
In LOOP5 iterating through traditional way is Maverick
In LOOP5 iterating through traditional way is Rock