Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Iterative Statement

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Iterative Statements:

 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.

Java provides the following iterative statements.

 while statement
 do-while statement
 for statement
 for-each statement

while statement in java


 The while statement is used to execute a single statement or block of statements
repeatedly as long as the given condition is TRUE.
 The while statement is also known as Entry control looping 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!

do-while statement in java


 This loop will execute the code block once. Then the condition will be evaluated, if the
condition is true the do block will be executed again. This process repeats until the
given condition becomes false. statement(s) may be a single statement or a block of
statements
 The do-while statement is also known as the Exit control looping statement.
Example Program:

public class DoWhileTest {

public static void main(String[] args) {

int num = 1;

do {
System.out.println(num);
num++;
}while(num <= 10);

System.out.println("Statement after do-while!");

Output:
1
2
3
4
5
6
7
8
9
10
Statement after do-while!

for statement in java


The for statement is used to execute a single statement or a block of statements repeatedly as
long as the given condition is TRUE
The execution begins with the initialization statement. After the initialization statement, it
executes Condition. If the condition is evaluated to true, then the block of statements executed
otherwise it terminates the for-statement. After the block of statements execution,
the modification statement gets executed,
for-each statement in java
 The Java for-each statement was introduced since Java 5.0 version.
 It provides an approach to traverse through an array or collection in Java.
 The for-each statement also known as enhanced for statement.
 The for-each statement executes the block of statements for each element of the given
array or collection.

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);
}
}
}

You might also like