Transcript statement
Iteration
Overview
while
for
do
statement
statement
while statement
1
while statement
It is very common in programming to have a
statement that needs to be executed more than once.
To avoid repeating the same statement in a
program, java provide three repetition (loop)
statements that may be used. These are while, for
and do-while.
while Statement:
The Syntax of the while statement is as follows:
while (condition)
statement;
Interpretation
» First evaluate the condition. If it is true,
the statement is executed; then the
condition is evaluated again
» The statement is executed over and over
until the condition becomes false
» If the condition is false initially, the
statement is not executed even once
» Therefore, we say that a while statement
executes its body zero or more times
2
while statement flow diagram
Flow diagram for while statement
condition
false
true
statement
3
while statement Example
The following example prints a table of squares for
integers from 1 to 10.
class WhileExample {
public static void main(String[] args) {
int i;
i = 0;
while (i < 10) {
i++;
System.out.println(i+ "\t" + Math.pow(i,
2));
}
}
}
4
Important points about while statement
1.
2.
3.
The condition of the while statement usually
involves at least one variable called loop control
variable.
The loop control variable should have a value (
initialized ) before entering the loop.
The loop control variable should be updated inside
the body of the loop such that the condition will
eventually evaluates to false, otherwise loop will
be an infinite loop.
public class Forever {
public static void main(String[] args) {
int i = 0;
while (i < 10)
System.out.println(”Never stop");
}
}
5
for statement
Because most loops involves three steps, namely,
initialization, checking the condition and updating
the control variable, Java provides the for statement
which summarizes them on one line.
The Syntax is:
for (initialization; condition; updating)
statements
Interpretation
» First initialization statement is executed
» Then the condition is evaluated
» If the condition is true,
– The statements inside the loop are
executed
– Then the updating statement is executed
– Then the condition is checked again, etc.
» If the condition is false,
– The control will proceed to the next
statement(s) after the for loop
6
For statement flow diagram
Flow diagram
for ( Initialization
Statement
{
condition
Updation
Statement
true
Statements inside the
for loop
}
false
7
)
for statement Example
The following example prints a table of squares for
integers from 1 to 10 using for loop.
class ForExample {
public static void main(String[]
args) {
for (int i = 1; i <= 10; i++)
System.out.println(i + "\t" +
Math.pow(i,2));
}
}
8
do while statement syntax
Some times it is more appropriate to perform the
test of a loop at the end. For this Java provides the
do-while statement.
The Syntax is:
do {
statements
} while ( condition ) ;
Interpretation
» First the statements inside the do loop are
executed regardless of the condition
» Then the condition is evaluated
» If it is true
– The statements inside the do loop are
executed again
– And the condition is checked again, etc.
» If condition is false
– The statement that follows the do while
statement is executed
9
do while statement flow diagram
Flow diagram
Statements inside
the do loop
true
Condition
false
10
do while statement Example
import java.io.*;
class DoExample {
public static void main(String[] args) throws
IOException {
BufferedReader stdin = new BufferedReader(
new InputStreamReader( System.in ) );
int num;
do {
System.out.println("Enter a Number or 0 to stop
execusion");
String input= stdin.readLine();
num = Integer.parseInt(input);
System.out.println("Squared number
is:"+(num*num));
} while (num != 0);
}
}
11