Download presentation
Presentation is loading. Please wait.
Published byLinda Wiggins Modified over 9 years ago
1
Repetition
2
Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION
3
Repetition - Used to repeat one or more programming statements. while Statement do-while Statement for Statement
4
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;
5
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
6
while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( ) { } Statement (loop body) Boolean Expression
7
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.
8
Control Flow of while int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; sum = sum + number; number = number + 1; true
9
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(); while (num < 0); //error-- semicolon System.out.print("Enter a value: "); num = input.nextInt(); Causes an infinite loop.
10
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; } 2 2 int product = 0; while ( product < 500000 ) { product = product * 5; } 1 1
11
while Loop Pitfall - 2 Goal: Execute the loop body 10 times. count = 1; while (count < 10) {... count++; } 1 1 count = 0; while (count <= 10) {... count++; } 3 3 count = 1; while (count <= 10) {... count++; } 2 2 count = 0; while (count < 10) {... count++; } 4 4 1 1 3 3 andexhibit off-by-one error.
12
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)
13
do { sum += number; number++; } while (sum <= 1000000); Syntax for the do-while Statement do { } while ( ); Statement (loop body) Boolean Expression
14
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.
15
Control Flow of do-while int sum = 0, number = 1 sum += number; number++; sum += number; number++; sum <= 1000000 ? true false
16
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
17
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.
18
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;
19
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
20
for ( i = 0 ; i < 20 ; i++ ) { number = inputBox.getInteger(); sum += number; } Syntax for the for Statement for ( ; ; ) Initialization Boolean Expression Update Statement (loop body)
21
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).
22
Control Flow of for i = 0; false number = inputBox.getInteger( ); sum += number; true i ++; i < 20 ?
23
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
24
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
25
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
26
Variable Trace int num1 = 0; int num2 = 0; while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1; System.out.print(num2 + " "); } num1 += 1; }
27
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; }
28
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; }
29
Homework Page 138 # 1, 2, 3, 5 Page 140 # 1 Page 141 # 3, 4 Page 143 # 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.