0% found this document useful (0 votes)
7 views

Looping statements

Uploaded by

khaledxxoxo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Looping statements

Uploaded by

khaledxxoxo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 5:

Repetition Structures (Loops)

Introduction to Java Programming

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

Trace while Loop


Initialize count
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
3
animation

Trace while Loop, cont.


(count < 2) is true
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
4
animation

Trace while Loop, cont.


Print Welcome to Java
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
5
animation

Trace while Loop, cont.


Increase count by 1
int count = 0; count is 1 now

while (count < 2) {


System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
6
animation

Trace while Loop, cont.


(count < 2) is still true since count
int count = 0; is 1

while (count < 2) {


System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
7
animation

Trace while Loop, cont.


Print Welcome to Java
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
8
animation

Trace while Loop, cont.


Increase count by 1
int count = 0; count is 2 now

while (count < 2) {


System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
9
animation

Trace while Loop, cont.


(count < 2) is false since count is 2
int count = 0; now

while (count < 2) {


System.out.println("Welcome to Java!");
count++;
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
10
animation

Trace while Loop


The loop exits. Execute the next
int count = 0; statement after the loop.

while (count < 2) {


System.out.println("Welcome to Java!");
count++;
}

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;

while (variable != sentinel)


{
.
.
.
input a 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

Trace for Loop


Declare i
int i;
for (i = 0; i < 2; i++) {
System.out.println(
"Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
21
animation

Trace for Loop, cont.


Execute initializer
int i; i is now 0
for (i = 0; i < 2; i++) {
System.out.println(
"Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
22
animation

Trace for Loop, cont.


(i < 2) is true
int i; since i is 0
for (i = 0; i < 2; i++) {
System.out.println( "Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
23
animation

Trace for Loop, cont.


Print Welcome to Java
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
24
animation

Trace for Loop, cont.


Execute adjustment statement
int i; i now is 1
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
25
animation

Trace for Loop, cont.


(i < 2) is still true
int i; since i is 1
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
26
animation

Trace for Loop, cont.


Print Welcome to Java
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
27
animation

Trace for Loop, cont.


Execute adjustment statement
int i; i now is 2
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
28
animation

Trace for Loop, cont.


(i < 2) is false
int i; since i is 2
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
29
animation

Trace for Loop, cont.


Exit the loop. Execute the next
int i; statement after the loop
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}

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:

for ( ; ; ) { Equivalent while (true) {


// Do something // Do something
} }
(a) (b)

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

for (int i=0; i<10; i++);


{
System.out.println("i is " + i);
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
32
Caution, cont.

Similarly, the following loop is also wrong:


Logic Error
int i=0;
while (i < 10);
{
System.out.println("i is " + i);
i++;
}

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

You might also like