Transcript Repetition

Repetition
Control of Flow

SEQUENCE

SELECTION (if..else, switch…case)

REPETITION
Repetition
- Used to repeat one or more programming
statements.



while Statement
do-while Statement
for Statement
The while Statement

In cooking, a recipe may tell us that, as there
are lumps in a sauce we should stir it.

In Java:
While (there are lumps)
give sauce a stir;
The while Statement
 Loop while loops continue to loop while some
condition is true, and stops when the condition
becomes false
 The condition is a Boolean expression
 Will never execute if the condition is initially
false
Syntax for the while Statement
while ( <boolean expression> ) {
<statement>
}
Boolean Expression
while (
sum
Statement
(loop body)
number <= 100
=
sum + number;
number = number + 1;
}
) {
The while Statement
int sum = 0, number = 1;
while ( number <= 100 ) {
sum
=
sum + number;
number = number + 1;
}
These statements are
executed as long as
number is less than or
equal to 100.
Control Flow of while
int sum = 0, number = 1
number <=
100 ?
false
true
sum = sum + number;
number = number + 1;
Infinite Loops
 A loop that continues executing forever
 Can be caused by syntax or logic errors. For example:
while (num < 0)
//error--no braces
System.out.print("Enter a value: ");
num = input.nextInt();
Causes an infinite loop.
while (num < 0);
//error-- semicolon
System.out.print("Enter a value: ");
num = input.nextInt();
while Loop Pitfall - 1
1
int product = 0;
while ( product < 500000 ) {
product = product * 5;
}
Infinite Loops
Both loops will not
terminate because the
boolean expressions will
never become false.
2
int count = 1;
while ( count != 10 ) {
count = count + 2;
}
while Loop Pitfall - 2

1
Goal: Execute the loop body 10 times.
count = 1;
2
while (count < 10) {
while (count <= 10) {
. . .
. . .
count++;
count++;
}
3
}
count = 0;
while (count <= 10) {
}
count = 1;
4
count = 0;
while (count < 10) {
. . .
. . .
count++;
count++;
}
1 and 3 exhibit off-by-one error.
The do-while Statement
 Alternative form of the while statement
 Executes at least once
 In do while loops the test condition comes at the
end of the loop. So, the loop always iterates at least
once.
In Cooking Example:
give sauce a stir;
while (there are lumps)
Syntax for the do-while Statement
do {
<statement>
} while (<boolean expression>);
do
{
sum += number;
number++;
} while (sum <= 1000000);
Boolean Expression
Statement
(loop body)
The do-while Statement
int sum = 0, number = 1;
do {
sum += number;
number++;
} while ( sum <= 1000000 );
These statements are
executed as long as sum
is less than or equal to
1,000,000.
Control Flow of do-while
int sum = 0, number = 1
sum += number;
number++;
sum <= 1000000 ?
false
true
Pre-test vs. Post-test loops

Use a pre-test loop for something that
may be done zero times

Use a post-test for something that is
always done at least once
Checklist for Repetition
Control
1.
2.
3.
Watch out for the off-by-one error
(OBOE).
Make sure the loop body contains a
statement that will eventually cause the
loop to terminate.
Make sure the loop repeats exactly the
correct number of times.
The for Statement
Returning one last time to our cooking
analogy, we might have instructions to
eliminate lumpiness phrased in the form”stir
mixture for 100 strokes”
 In Java:
int count;
for (count = 0; count < 100; count++)
give sauce a stir;

The for Statement
 Loop structure that executes a set of statements a
fixed number of times
 Uses a loop control variable (lcv)
 The increment (++) or decrement (--) operators are
used to change the value of the loop control variable
Syntax for the for Statement
for ( <initialization>; <boolean expression>; <update>
)
<statement>
Boolean
Expression
Initialization
for (
i = 0
;
i < 20
number = inputBox.getInteger();
sum += number;
}
Update
;
i++
) {
Statement
(loop body)
The for Statement
int i, sum = 0, number;
for (i = 0; i < 20; i++) {
number = In.getInt();
sum += number;
}
These statements are
executed for 20 times
( i = 0, 1, 2, … , 19).
Control Flow of for
i = 0;
false
i < 20 ?
true
number = inputBox.getInteger( );
sum
+= number;
i ++;
The Nested-for Statement

If a first loop contains a second loop, we
say the second is nested within the first.

The first loop is often referred to as the
outer loop and the second as the inner
loop.

Read page 152-153
Which Loop, When?
for loop – when you know how many times the
loop will be executed
while loop – when you don’t know how many
times the loop will execute, including when
the user is in control
while loop – when you may not want the loop
to execute even once
do while – when you want the loop to execute
even once
Debugging Techniques
 The debugger included with many compilers
 Variable trace, which is a manual technique of list
values of variables at the points of assignment
 Additional println() statements for displaying variable
values at points of assignment
 "Commenting out" code to detect bugs through a
process of elimination
Variable Trace
int num1 = 0;
int num2 = 0;
while (num1 < 10) {
if (num1 % 3 == 0) {
num2 += num1;
System.out.print(num2 + " ");
}
num1 += 1;
}
Using println() to Debug
int num1 = 0;
int num2 = 0;
System.out.println("num1 before while: " + num1); //debug
while (num1 < 10) {
System.out.println("num1 in while: " + num1);
//debug
if (num1 % 3 == 0) {
num2 += num1;
System.out.println("num2: " + num2);
//debug
System.out.print(num2 + " ");
}
num1 += 1;
}
Using Comments to Debug
int num1 = 0;
int num2 = 0;
while (num1 < 10) {
//if (num1 % 3 == 0) {
//
num2 += num1;
//
System.out.print(num2 + " ");
//}
num1 += 1;
}
Homework




Page 138 # 1, 2, 3, 5
Page 140 # 1
Page 141 # 3, 4
Page 143 # 1