Iteration Control Structures
Iteration Control Structures
while loop
do-while loop
To write programs for executing
statements repeatedly.
To create a for statement
To create a while statement
To create a do-while statement
Java’s iteration statements are for, while and do-while.
These statements create loops. A loop is a structure
that allows repeated execution of a block of
statements. Within a looping structure, a Boolean
expression (loop-continuation condition) is evaluated.
If it is true, a block of statements called the loop body
executes and the Boolean expression is evaluated
again. One execution of any loop is called iteration.
Requirements in using iteration structure
a control variable (or loop counter)
the initial value of the control variable
the increment/decrement/input by which the control
variable is modified each time through the loop (also
known as each iteration of the loop)
the loop-continuation condition that determines if
looping should continue.
A for loop is ideal for performing a known number
of iterations.
You begin a for loop with the keyword for followed
by a set of parentheses. Within the parentheses are
three sections separated by exactly two semicolons.
The three sections are usually used for the following:
• Initializing the loop control variable
• Testing the loop control variable
• Updating the loop control variable
Syntax:
for(initial-action; loop-continuation-condition; action-after-each-iteration){
//loop body
Statement(s);
}
Here is an example of a simple for loop that prints “Hello”
five times:
int count;
for (count = 1; count <= 5; count++)
System.out.println("Hello");
Output:
Hello
Hello
Hello
Hello
Hello
int count;
for (count = 1; count <= 5; count++)
System.out.println("Hello");
Output:
Hello
Hello
Hello
Hello
Hello
Sequence of events in the for loop
Logic of the for loop
a b c
0 -5 5
-4
2 -3 8
4 -2 11
6 -1 14
0
8 1 17
10 2
3
4
A while loop executes statements repeatedly while the
condition is true.
Syntax:
while (loop-continuation-condition) {
// Loop body
Statement(s);
}
Here is an example of a simple while loop that prints “Hello” five times:
int number = 1;
while (number <= 5)
{
System.out.println("Hello");
number++;
}
Output:
Hello
Hello
Hello
Hello
Hello
How many times will "Hello World" be printed in the
following program segment?
int count = 10;
while (count < 1)
{
System.out.println("Hello World");
count++;
}
How many times will "I love Java programming!" be printed in the following
program segment?
int count = 0;
while (count < 10)
System.out.println("Hello World! ");
System.out.println("I love Java programming! ");
A do-while loop is the same as a while loop except
that it executes the loop body first and then checks the
loop continuation condition.
Syntax:
do{
// Loop body;
Statement(s);
}while(loop-continuation-condition);
Here is an example of a simple do while loop that prints “Hello” five times:
int number = 1;
do{
System.out.println("Hello");
number++;
}while(number <= 5);
Output:
Hello
Hello
Hello
Hello
Hello
What does the following program segment output?
int d = 0;
do{
System.out.print(d + " ");
d++;
}while(d < 2);