repetition. control of flow sequence selection (if..else, switch…case) repetition
TRANSCRIPT
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
while ( number <= 100 ) {
sum = sum + number;
number = number + 1;
}
Syntax for the while Statementwhile ( <boolean expression> ) {
<statement>
}
Statement(loop body)
Statement(loop body)
Boolean ExpressionBoolean Expression
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.
These statements are executed as long as number is less than or equal to 100.
Control Flow of while
int sum = 0, number = 1int sum = 0, number = 1
number <= 100 ?
number <= 100 ?
falsesum = sum + number;
number = number + 1;
sum = sum + number;
number = number + 1;
true
Infinite Loops
A loop that continues executing forever
Can be caused by syntax or logic errors. For example:
while (num < 0) //error--no bracesSystem.out.print("Enter a value: ");num = input.nextInt();
while (num < 0); //error-- semicolonSystem.out.print("Enter a value: ");num = input.nextInt();
Causes an infinite loop.
while Loop Pitfall - 1
Infinite Loops Both loops will not terminate because the boolean expressions will never become false.
Infinite Loops Both loops will not terminate because the boolean expressions will never become false.
int count = 1;
while ( count != 10 ) {
count = count + 2;
}
22
int product = 0;
while ( product < 500000 ) {
product = product * 5;
}
11
while Loop Pitfall - 2 Goal: Execute the loop body 10 times.
count = 1;
while (count < 10) {
. . .
count++;
}
11
count = 0;
while (count <= 10) {
. . .
count++;
}
33
count = 1;
while (count <= 10) {
. . .
count++;
}
22
count = 0;
while (count < 10) {
. . .
count++;
}
44
11 33and 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)
do {
sum += number;
number++;
} while (sum <= 1000000);
Syntax for the do-while Statement
do {
<statement>
} while (<boolean expression>);
Statement(loop body)
Statement(loop body)
Boolean ExpressionBoolean Expression
The do-while Statementint 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.
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 = 1int sum = 0, number = 1
sum += number;
number++;
sum += number;
number++;
sum <= 1000000 ?sum <= 1000000 ?true
false
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. Watch out for the off-by-one error (OBOE).
2. Make sure the loop body contains a statement that will eventually cause the loop to terminate.
3. 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
for ( i = 0 ; i < 20 ; i++ ) {
number = inputBox.getInteger();
sum += number;
}
Syntax for the for Statementfor ( <initialization>; <boolean expression>; <update> )
<statement>
InitializationInitialization Boolean Expression
Boolean Expression UpdateUpdate
Statement(loop body)
Statement(loop body)
The for Statementint 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).
These statements are executed for 20 times ( i = 0, 1, 2, … , 19).
Control Flow of for
i = 0;i = 0;
false
number = inputBox.getInteger( );sum += number;
number = inputBox.getInteger( );sum += number;
true
i ++;i ++;
i < 20 ? i < 20 ?
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); //debugwhile (num1 < 10) {System.out.println("num1 in while: " + num1); //debugif (num1 % 3 == 0) {num2 += num1;System.out.println("num2: " + num2); //debugSystem.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