Download presentation
Presentation is loading. Please wait.
1
Looping and Repetition
SE-1010 Dr. Mark L. Hornick
2
Looping and Repetition Statements control a block of code to be executed for:
a fixed number of times 1, 10, 10000… until a certain condition is met a value remains positive (or negative) a value remains below (or above) some limit … SE-1010 Dr. Mark L. Hornick
3
The two (three) main loop types:
while() and it’s cousin do…while() Sentinel-controlled loops, where the loop body is executed repeatedly until a designated condition, called a sentinel, is encountered. for() A count-controlled loop, where the loop body is executed a fixed number of times. SE-1010 Dr. Mark L. Hornick
4
The do-while() Statement
Format: do { <statements> } while (<boolean expression>); The <statements> part is known as the loop body The loop body is executed until the <boolean expression> becomes false And is always executed at least once! Even if <boolean expression> is false SE-1010 Dr. Mark L. Hornick
5
The do-while() statement
Demo SE-1010 Dr. Mark L. Hornick
6
The while Statement Format:
while ( <boolean expression> ){ <statements that repeat> // zero or more } As long as the <boolean expression> is true, the loop body is executed SE-1010 Dr. Mark L. Hornick
7
The while() statement Demo SE-1010 Dr. Mark L. Hornick
8
while vs. do-while The while statement is a pre-test loop
because the test is done before the loop body is entered and executed Therefore, the loop body may not be executed if the test is false to begin with. The do-while statement is a post-test loop. With a post-test loop statement, the loop body is executed at least once. SE-1010 Dr. Mark L. Hornick
9
while vs. do-while int x=0, y=1; while(x==y) { // never true
//statements never execute }; do { //statement executes once } while(x==y); // never true SE-1010 Dr. Mark L. Hornick
10
The break statement terminates a loop early, in the middle of the loop body
int i=0; do { i=i+1; if( i>5 ) break; // quit early } while( i<100 ); <more statements> SE-1010 Dr. Mark L. Hornick
11
The continue statement causes the loop to repeat immediately, without processing the remaining statements int i=0; do { i=i+1; if( i>5 ) continue; // skip remaining code <other statements to be repeated> } while( i<100 ); <more statements> SE-1010 Dr. Mark L. Hornick
12
Common mistakes when using loops:
Identify the error: int product = 0; while (product < 5000) { product = product * 5; } SE-1010 Dr. Mark L. Hornick
13
The infinite loop pitfall
Identify the error: int product = 0; while (product < 5000) { product = product * 5; } Because product is initialized to 0, product will never be larger than 5000 (0 = 0 * 5), so the loop will never terminate. SE-1010 Dr. Mark L. Hornick
14
Identify the error: int count = 1; while (count != 10) {
count = count + 2; } SE-1010 Dr. Mark L. Hornick
15
The Overflow pitfall Identify the error:
int count = 1; while (count != 10) { count = count + 2; } Here, count will never equal 10 (1,3,5,7,9,11…) …so the while() loop runs forever Q: How large does count get?? SE-1010 Dr. Mark L. Hornick
16
About Overflow errors…
An overflow error occurs when you attempt to assign a value larger than the maximum value the variable can hold. In Java, an overflow does not cause program termination. With types float and double, a value that represents infinity is assigned to the variable. With type int, the value “wraps around” and becomes a negative value. SE-1010 Dr. Mark L. Hornick
17
Identify the error: float count = 1.3f; while (count != 100003.0) {
count = count + 1.0; } SE-1010 Dr. Mark L. Hornick
18
The Floating-point roundoff error
Identify the error: float count = 1.0F; while (count != ) { count = count + 1.0; } Real numbers should not be used in testing or increment, because only an approximation of real numbers can generally be stored in a computer. SE-1010 Dr. Mark L. Hornick
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.