Iterative Statement
Iterative Statement
Iterative Statement
The iterative statements are also known as looping statements or repetitive statements.
The iterative statements that are used to execute a statement or a block of statements
repeatedly as long as the given condition is true.
while statement
do-while statement
for statement
for-each statement
First evaluate the condition if the condition is true execute the body of the loop and
again test the condition it is true execute the body of the loop again. Repeated the
process until test condition becomes false. When the condition is false terminate the
loop.
Example Program:
class WhileTest
{
public static void main(String[] args)
{
int num = 1;
while(num <= 10)
{
System.out.println(num);
num++;
}
System.out.println("Statement after while!");
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Statement after while!
int num = 1;
do {
System.out.println(num);
num++;
}while(num <= 10);
Output:
1
2
3
4
5
6
7
8
9
10
Statement after do-while!
Drawback:
The drawback of the enhanced for loop is that it cannot traverse the elements in reverse
order.
You do not have the option to skip any element.
Example Program:
class ForEach
{
public static void main(String args[])
{
int arr[]={12,13,14,44};
for(int i:arr)
{
System.out.println(i);
}
}
}