In this section, we will discuss about for loop in java 7. This is one type of loop statement.
For Loop Statement in java 7
In this section, we will discuss about for loop in java 7. This is one type of loop statement.
For Loop Statements :
For loop is one way of looping to iterate block of code under certain condition. It contains three parts
- Initialization
- Condition/Termination
- Increment/Decrement
Initialization : In this phase of for loop we assign some value to the variable. It is done at the beginning of your for loop. We take this initialized value as the starting point of the loop.
Condition/Termination : To break the loop it is required to put any condition in your loop. This condition decides whether the loop will continue running or not. If the specified condition is true, the loop will continue executing the block of code otherwise it breaks the execution. This is also called termination point.
Increment/Decrement : It is your iteration part, you can increment or decrement loop variable which changes the value of variable so that it meets to the condition.
Syntax:
for(initialization; Boolean termination; increment/decrement)
{.......
...... //Statements
}
Example :
Here is simple for loop example. First we initialized the variable i =1 and iterate it till i<=10. Each time increment by 1.
package looping; class ForLoopStatement { public static void main(String[] args) { int i; System.out.println("For Loop Example : "); // for loop for (i = 1; i <=10; i++) { System.out.println("Hello " + i); } } }
Output :
For Loop Example : Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10