Iteration.pptx
Iteration.pptx
Programming in Java
Iteration
Rabea Khatun
Lecturer
Dept. Of CSE
East West University
System.out.println("Welcome to
Java!");
The while and do-while loops are also called conditional loops
since they use boolean expressions to control the loop behavior
The while and do-while loops run un-determined (unknown)
number of iterations (some call them non-deterministic loops)
The for loop, on the other hand, runs a pre-determined
(known) number of iterations (some call it deterministic loop or
counting loop)
Statement
Block conditio
n
Loop body evaluate
d
tru fals
tru
e e e
conditio Statement
n block
evaluate
d
Next Line
do-while Loop Example
• An example of a do loop:
int count = 0;
do
{
count = count +1;
System.out.println (count);
} while (count < 5);
While Loop
initializatio
conditio
n n
evaluate
conditio d
n tru fals
evaluate e e
d statement block
tru fals
e e
statement
block
Like a while loop, the condition of a for
increme
nt loop is tested prior to executing the loop
body. Therefore, the for loop body will
execute zero or more times
Trace for Loop
Declare i
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Execute initializer
i is now 0
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
(i < 2) is true
since i is 0
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Print Welcome to Java
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Execute adjustment statement
i now is 1
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
(i < 2) is still true
since i is 1
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Print Welcome to Java
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Execute adjustment statement
i now is 2
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
(i < 2) is false
since i is 2
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Trace for Loop, cont.
Exit the loop. Execute the next
statement after the loop
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
for Loop as a while Loop
initialization;
while (condition)
{
statement block;
increment;
}
Infinite Loops
• The body of a while loop eventually must make
the condition false
• If not, it is called an infinite loop, which will execute
until the user interrupts the program
• This is a common logical error
• You should always double check the logic of a
program to ensure that your loops will terminate
normally
Example
int count1 = 1;
while (count1 <= 10)
{
int count2 = 1;
while (count2 <= 5)
{
System.out.println("I am here!");
count2 = count2 + 1;
}
System.out.println(); // blank line
count1 = count1 + 1;
}
}
}
Control Statement(Jump)
Break
Break cause immediate termination of execution of a iterative
statement.
Level Break
A level break cause immediate termination of execution of a
program portion and put control to a portion after that level. For
example
}
}