Looping and Repetition SE1010 Dr Mark L Hornick

  • Slides: 18
Download presentation
Looping and Repetition SE-1010 Dr. Mark L. Hornick 1

Looping and Repetition SE-1010 Dr. Mark L. Hornick 1

Looping and Repetition Statements control a block of code to be executed for: a

Looping and Repetition Statements control a block of code to be executed for: a fixed number of times l 1, 10000… until a certain condition is met l l l a value remains positive (or negative) a value remains below (or above) some limit … SE-1010 Dr. Mark L. Hornick 2

The two (three) main loop types: while() and it’s cousin do…while() l Sentinel-controlled loops,

The two (three) main loop types: while() and it’s cousin do…while() l Sentinel-controlled loops, where the loop body is executed repeatedly until a designated condition, called a sentinel, is encountered. for() l A count-controlled loop, where the loop body is executed a fixed number of times. SE-1010 Dr. Mark L. Hornick 3

The do-while() Statement Format: do { <statements> } while (<boolean expression>); The <statements> part

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 l And is always executed at least once! Even if <boolean expression> is false SE-1010 Dr. Mark L. Hornick 4

The do-while() statement l Demo SE-1010 Dr. Mark L. Hornick 5

The do-while() statement l Demo SE-1010 Dr. Mark L. Hornick 5

The while Statement Format: while ( <boolean expression> ){ <statements that repeat> // zero

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 6

The while() statement l Demo SE-1010 Dr. Mark L. Hornick 7

The while() statement l Demo SE-1010 Dr. Mark L. Hornick 7

while vs. do-while The while statement is a pre-test loop l l because the

while vs. do-while The while statement is a pre-test loop l l 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. l With a post-test loop statement, the loop body is executed at least once. SE-1010 Dr. Mark L. Hornick 8

while vs. do-while int x=0, y=1; while(x==y) { // never true //statements never execute

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 9

The break statement terminates a loop early, in the middle of the loop body

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 10

The continue statement causes the loop to repeat immediately, without processing the remaining statements

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 11

Common mistakes when using loops: Identify the error: int product = 0; while (product

Common mistakes when using loops: Identify the error: int product = 0; while (product < 5000) { product = product * 5; } SE-1010 Dr. Mark L. Hornick 12

The infinite loop pitfall Identify the error: int product = 0; while (product <

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 13

Identify the error: int count = 1; while (count != 10) { count =

Identify the error: int count = 1; while (count != 10) { count = count + 2; } SE-1010 Dr. Mark L. Hornick 14

The Overflow pitfall Identify the error: int count = 1; while (count != 10)

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 15

About Overflow errors… An overflow error occurs when you attempt to assign a value

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 16

Identify the error: float count = 1. 3 f; while (count != 100003. 0)

Identify the error: float count = 1. 3 f; while (count != 100003. 0) { count = count + 1. 0; } SE-1010 Dr. Mark L. Hornick 17

The Floating-point roundoff error Identify the error: float count = 1. 0 F; while

The Floating-point roundoff error Identify the error: float count = 1. 0 F; while (count != 1000. 0) { 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 18