Looping statements
Looping statements
Eleventh Edition
by Liang
while Loop Flow Chart
int count = 0;
while (loop-continuation-condition) {
while (count < 100) {
// loop-body;
System.out.println("Welcome to Java!");
Statement(s);
count++;
} }
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
2
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
3
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
4
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
5
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
6
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
7
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
8
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
9
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
10
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
11
Infinite Loops
In order for a while loop to end, the condition
must become false. The following loop will not
end:
int x = 20;
while(x > 0)
{
System.out.println("x is greater than 0");
}
The variable x never gets decremented so it will
always be greater than 0.
Adding the x-- above fixes the problem.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
12
Infinite Loops
Thisversion of the loop decrements x
during each iteration:
int x = 20;
while(x > 0)
{
System.out.println("x is greater than 0");
x--;
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
13
Counter-Controlled while Loop
Counter-Controlled while Loop.
Sentinel-Controlled while Loop.
Flag-Controlled while Loop.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
14
Counter-Controlled while Loop
Used when exact number of data or entry pieces is
known.
General form:
int N = //value input by user or specified
//in program
int counter = 0;
while (counter < N)
{
.
.
.
counter++;
.
.
.
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
15
Sentinel-Controlled while Loop
Used when exact number of entry pieces is unknown, but
last entry (special/sentinel value) is known.
General form:
Input the first data item into variable;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
16
Sentinel-Controlled while Loop Example
import javax.swing.JOptionPane;
...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int number, sum, count;
count = 0;
sum = 0;
number = Integer.valueOf(JOptionPane.showInputDialog("Enter
any number, or 0 to stop"));
while (number != 0) {
sum=sum+number;
count++;
number = Integer.valueOf(JOptionPane.showInputDialog(
"Enterany number, or 0 to stop"));
}
JOptionPane.showMessageDialog(null, "The sum of numbers =
"+sum, "Sum",JOptionPane.INFORMATION_MESSAGE);
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
17
Flag-Controlled while Loop
Boolean value used to control loop.
General form:
boolean found = false;
while (!found)
{
.
.
.
if (expression)
found = true;
.
.
.
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
18
Flag-Controlled while Loop Example
import javax.swing.JOptionPane;
...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
boolean nonNegative;
int sum = 0;
int number;
nonNegative = true; //initialize flag
while (nonNegative) { //test flag: default test done for true
number = double val=Double.valueOf(JOptionPane.showInputDialog
(" Enter a number(negative to stop) "));
if (number < 0) //test the condition to change the flag
nonNegative = false; //(reset)update flag
else
sum = sum + number;
}
JOptionPane.showMessageDialog(null, "The sum of numbers =
"+sum, "Sum",JOptionPane.INFORMATION_MESSAGE);
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
19
for Loops
for (initial-action; loop- int i;
continuation-condition; action- for (i = 0; i < 100; i++) {
after-each-iteration) {
System.out.println(
// loop body;
Statement(s); "Welcome to Java!");
} }
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
20
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
21
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
22
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
23
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
24
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
25
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
26
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
27
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
28
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
29
animation
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
30
Note
If the loop-continuation-condition in a for loop is omitted,
it is implicitly true. Thus the statement given below in (a),
which is an infinite loop, is correct. Nevertheless, it is
better to use the equivalent loop in (b) to avoid confusion:
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
31
Caution
Adding a semicolon at the end of the for clause before
the loop body is a common mistake, as shown below:
Logic
Error
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
32
Caution, cont.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
33
Nested Loops
Like if statements, loops can be nested.
If a loop is nested, the inner loop will execute all of its
iterations for each time the outer loop executes once.
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(" *");
System.out.println();
}
Output:
*
**
***
****
*****
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
34