Iterative Statements in JAVA
Loop – It is the process of repeating a block of statements till the
condition is true.
for loop
It allows a set of statements to repeat for a fixed number of times
depending on the condition.
Syntax:
for (initialization ; condition; increment/decrement)
{
statement(s)
}
Initialization: Here, we initialize the loop variable with a
valid value. It marks the start of a for loop. Initialization
takes place only once for any loop.
Condition: It is used for testing the exit condition for a loop.
Till the condition is true the set of statements keeps on
repeating and when the condition becomes false, the loop
terminates.
Statements: If the condition is evaluated to true, the
statements in the loop body are executed.
Increment/Decrement: It is used for updating the loop
variable for next iteration. The value can either increase or
decrease.
Example 1: Example 2:
for (int i=1 ; i<=5 ; i++) for (int i=2 ; i<11; i=i+2)
{ {
System.out.println( i ); System.out.println( i );
} }
Output: Output:
1 2
2 4
3 6
4 8
5 10
Example 3: Example 4:
for (int i=9 ; i>=1; i=i-2) for (int i=5 ; i>1; i--)
{ {
System.out.println( i ); System.out.println( i );
} }
Output: Output:
9 5
7 4
5 3
3 2
1
Example 5: Example 6:
for (int i=1 ; i<5; i++) for (int i=7 ; i>=0; i=i-3)
{ {
System.out.println( i*i ); System.out.println( i -1);
} }
Output: Output:
1 6
4 3
9 0
16