Download presentation
Presentation is loading. Please wait.
Published byΆλκηστις Στεφανόπουλος Modified over 6 years ago
1
Introduction to Object-Oriented Programming with Java--Wu
Chapter 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. There are basically two types of terminating the loop: 1. count-controlled and 2. sentinel-controlled. Count-controlled loops terminate the execution of the loop after the loop body is executed for a fixed number of times. Sentinel-controlled loops terminate the execution of the loop after one of the designated values called a sentinel is encountered. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
2
Syntax for the while Statement
Chapter 7 Syntax for the while Statement while ( <boolean expression> ) { <statement> } Boolean Expression while ( number <= ) { sum = sum + number; number = number + 1; } Statement (loop body) © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
3
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 Control Flow of while int sum = 0, number = 1 number <= 100 ? sum = sum + number; number = number + 1; true false © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
4
Introduction to Object-Oriented Programming with Java--Wu
Example Programs Puuuurginooo … out to reality … While10000.java Puuuurginooo … out to reality … WhileInput.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu
5
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 while Loop Pitfall - 1 int product = 0; while ( product < ) { product = product * 5; } 1 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 Note: In theory, this while statement is an infinite loop, but in programming languages other than Java, this loop will eventually terminate because of an overflow error. An overflow error will occur if you attempt to assign a value larger than the maximum value the variable can hold. When an overflow error occurs, the execution of the program is terminated in almost all programming languages. With Java, however, an overflow will not cause the program termination. When an overflow occurs in Java, a value that represents infinity (IEEE 754 infinity, to be precise) is assigned to a variable and no abnormal termination of a program will happen. Also, in Java an overflow occurs only with float and double variables; no overflow will happen with int variables. When you try to assign a value larger than the maximum possible integer an int variable can hold, the value “wraps around” and becomes a negative value. Whether the loop terminates or not because of an overflow error, the logic of the loop is still an infinite loop, and we must watch out for it. When you write a loop, you must make sure that the boolean expression of the loop will eventually become false. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
6
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 while Loop Pitfall - 2 float count = 0.0f; while ( count != 1.0f ) { count = count f; } 1 Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. float count = 0.0f; while ( count <= 1.0f ) { count = count f; } 2 Although 1/3 + 1/3 + 1/3 == 1 is mathematically true, the expression 1.0/ / /3.0 in computer language may or may not get evaluated to 1.0 depending on how precise the approximation is. In general, avoid using real numbers as counter variables because of this imprecision. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
7
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 while Loop Pitfall - 3 Goal: Execute the loop body 10 times. count = 1; while (count < 10) { . . . count++; } 1 count = 1; while (count <= 10) { . . . count++; } 2 count = 0; while (count <= 10) { . . . count++; } 3 count = 0; while (count < 10) { . . . count++; } 4 Yes, you can write the desired loop as count = 1; while (count != 10 ) { ... count++; } but this condition for stopping the count-controlled loop is dangerous. We already mentioned about the potential trap of an infinite loop. 1 3 and exhibit off-by-one error. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
8
Introduction to Object-Oriented Programming with Java--Wu
Example Programs Grunk … out to reality … WhileLog.java Grunk … out to reality … WhileBoolean.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu
9
The do-while Statement
Chapter 7 The do-while Statement int sum = 0, number = 1; do { sum += number; number++; } while ( sum <= ); These statements are executed as long as sum is less than or equal to 1,000,000. The following is the routine presented earlier that inputs a person’s age using the do–while statement. do { age = inputBox.getInteger("Your Age (between 0 and 130):"); if (age < 0 || age > 130) { messageBox.show("An invalid age was entered. " + "Please try again."); } } while (age < 0 || age > 130); This code is not as good as the version using the while statement it includes an if statement inside its loop body and the if test is repeating the same boolean expression of the do–while. Since the loop body is executed repeatedly, it is important not to include any extraneous statements. Moreover, duplicating the testing conditions tends to make the loop statement harder to understand. For this example, we can avoid the extra test inside the loop body and implement the control flow a little more clearly by using a while statement. In general, the while statement is more frequently used than the do–while statement. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
10
Syntax for the do-while Statement
Chapter 7 Syntax for the do-while Statement do { <statement> } while (<boolean expression>); do { sum += number; number++; } while (sum <= ); Statement (loop body) Boolean Expression © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
11
Control Flow of do-while
Chapter 7 Control Flow of do-while int sum = 0, number = 1 sum += number; number++; true sum <= ? false © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
12
Introduction to Object-Oriented Programming with Java--Wu
Example Programs Orque … out to reality … DoWhileInput.java Orque … out to reality … DoWhileDrink.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu
13
Pre-test vs. Post-test loops
Chapter 7 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 © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
14
Checklist for Repetition Control
Chapter 7 Checklist for Repetition Control 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. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
15
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = inputBox.getInteger(); sum += number; } These statements are executed for 20 times ( i = 0, 1, 2, … , 19). © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
16
Syntax for the for Statement
Chapter 7 Syntax for the for Statement for ( <initialization>; <boolean expression>; <update> ) <statement> Initialization Boolean Expression Update for ( i = 0 ; i < ; i ) { number = inputBox.getInteger(); sum += number; } The <initialization> component also can include a declaration of the control variable. We can do something like this: for (int i = 0; i < 10; i++) instead of int i; for (i = 0; i < 10; i++) Statement (loop body) © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
17
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 Control Flow of for i = 0; i < 20 ? false number = inputBox.getInteger( ); sum = number; true i ++; © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
18
Introduction to Object-Oriented Programming with Java--Wu
Example Programs Poodyplat … out to reality … ForPrint.java Poodyplat … out to reality … ForPower.java Poodyplat … out to reality … ForFibonacci.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu
19
The Nested-for Statement
Chapter 7 The Nested-for Statement Nesting a for loop inside another is a common technique Generate the following table using nested-for statements. ForTable.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
20
Indefinite vs. Definite loops
Chapter 7 Indefinite vs. Definite loops For loops and while loops are exchangeable but Use a for loop when the number of iterations is definite Use a while or do-while when the number of iterations depends on statements in the loop body © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.