Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Statements

Similar presentations


Presentation on theme: "Repetition Statements"— Presentation transcript:

1 Repetition Statements
Chapter 6 Repetition Statements CS 180 Recitation 27/28 September 2007 Department of Computer Science Purdue University

2 Announcements Project 4 due 3 October 2007
If you haven't started the second part, you should. (Projects now require starting on time) Level of cooperation Do not look at someone else's code or talk to someone else about specific code for a project. You may discuss general problem solving approaches. Talking to a TA about code is fine. We will study two forms of repetition statements in this lesson. They are while and do-while statement.

3 Announcements Mid Semester Evaluations
Deadline is Monday, October Monday To participate: log in using your Purdue career account ID and password, click on "Course Evaluations", and then click on "Mid-Semester Evaluations".

4 Syntax for the while Statement
while ( <boolean expression> ) <statement block> Boolean Expression while ( number <= ) { sum = sum + number; number = number + 1; } Statement (loop body) Here’s the general syntax of a while statement. As long as the <boolean expression> is true, the loop body is executed. Notice that the loop body may not be executed at all.

5 Control Flow of while true false sum = sum + number;
previous statement; int sum = 0, number = 1 number <= 100 ? true sum = sum + number; number = number + 1; This flowchart shows the control flow of the while statement. If the <boolean expression> is true, the loop body is executed and the control returns to the top. If the <boolean expression> is false, then the control flows to the next statement that follows this while statement. false next statement;

6 Syntax for the do-while Statement
<statement> while ( <boolean expression> ) ; do { sum += number; number++; } while ( sum <= ); Statement (loop body) Here’s the general syntax of a do-while statement. As long as the <boolean expression> is true, the loop body is executed. Notice that the loop body is executed at least once. Boolean Expression

7 Control Flow of while true false sum += number; number++;
previous statement; int sum = 0, number = 1 sum += number; number++; This flowchart shows the control flow of the while statement. If the <boolean expression> is true, the loop body is executed and the control returns to the top. If the <boolean expression> is false, then the control flows to the next statement that follows this while statement. number <= 100 ? true false next statement;

8 for statement syntax for ( <initialization>; <boolean expression>; <increment> ) <statement> Initialization Boolean Expression Increment for ( i = 0 ; i < ; i ) { number = scanner.nextInt(); sum += number; } Statement (loop body) This shows the general syntax for the for statement. 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++)

9 Control flow of for false true i < 20? sum += number; number++;
previous statement; i=0; i < 20? false true This flowchart shows the control flow of the while statement. If the <boolean expression> is true, the loop body is executed and the control returns to the top. If the <boolean expression> is false, then the control flows to the next statement that follows this while statement. sum += number; number++; next statement; i++;

10 Query Are the different forms of loops equivalent?
Could you use code to write them in terms of each other?

11 Watch Out for Pitfalls Watch out for the off-by-one error (OBOE).
Move your loop condition towards termination. Make sure the loop repeats exactly the correct number of times.

12 Overflow An infinite loop often results in an overflow error.
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 integral types, the value “wraps around” and becomes a negative value. With floating point, precision will determine whether or not true overflow occurs. (either infinite or unchanging) [See Overflow.java] When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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.

13 CAUTION: Empty loops! An inadvertent semi-colon can mean an empty loop! while ( name != null ); { someInt = JoptionPane.showInputDialog(null, “Enter Int”); i = someInt.parseInt(someInt); } for ( int i=0; i<10; i++ ); { name = JoptionPane.showInputDialog(null, ”Enter Int"); j += name.parseInt(name); }

14 Example 1 The charAt( int ) method of the String class returns the character at the given index of an instance. public String someMeth1( String str ) { String result = ""; for ( int index = str.length() - 1; index >= 0; --index ) result += str.charAt( index ); } return result; When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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. What is the result of: someMeth1( "somewhere i have never traveled" )

15 Example 2 How does the above compare to using a while loop?
public void someMeth2() { String name = ""; Scanner keyboard = new Scanner( System.in ); do System.out.print ("Please enter a name or \"quit\" to exit: "); name = keyboard.next(); System.out.println( "Howdy "+name+"!" ); } while ( !name.equals( "quit" ) ); } When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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. How does the above compare to using a while loop? Is a for loop appropriate?

16 Example 3 Does the above loop terminate?
public void someMeth3() { String name = ""; Scanner keyboard = new Scanner( System.in ); while ( true ) System.out.print ("Please enter a name or \"quit\" to exit: "); name = keyboard.next(); if ( !name.equals( "quit" ) ) System.out.println( "Howdy "+name+"!" ); else break; } When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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. Does the above loop terminate? How does this compare to someMeth2?

17 Example 4 Notice that this has a cleaner flow than example 3.
public void someMeth4() { S.o.p( "Please enter a name or 'quit' to exit: " ); Scanner keyboard = new Scanner( System.in ); String name = keyboard.next(); while ( !name.equals("quit") ) S.o.pln( "Howdy "+name+"!" ); S.o.p("Please enter a name or 'quit' to exit: "); name = keyboard.next(); } When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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. Notice that this has a cleaner flow than example 3. Priming the loop can avoid extra cases and bugs.

18 Example 5 How does this loop behave when 7 is entered? 3? 0?
public void someMeth5() { Scanner keyboard = new Scanner( System.in ); while ( true ) switch( keyboard.nextInt() ) case 0: case 1: case 2: System.out.println( "In here" ); break; case 3: case 4: case 5: System.out.println( "Somewhere else" ); continue; } System.out.println( "After Switch" ); When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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. How does this loop behave when 7 is entered? 3? 0?

19 Example 6 Notice how loops within loops can interact.
public void someMeth6() { for ( int i = 0; i < 5; ++i ) for ( int j = 5-i; j > 0; --j ) System.out.print( "*" ); System.out.println(); } Notice how loops within loops can interact. What does each loop correspond to? Notice how using a table can help you execute this manually. When an overflow error occurs, the execution of the program is terminated in almost all programming languages. 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.

20 Example 7 What happens here? Where is the mistake?
public void someMeth7() { for ( int i = 0; i < 5; ++i ) for ( int j = 5-i; j > 0; --i ) System.out.print( "*" ); System.out.println(); } What happens here? Where is the mistake?

21 Example 8 How does this behave? public void someMeth8() {
for ( int i = 0; i < 5; ++i ) for ( int j = 5-i; j > 0; --j ) System.out.print( "*" ); --i; System.out.println(); } How does this behave?

22 Example 9 How does this behave? What have we forgotten?
public void someMeth9() { int sum = 0; for ( int count = 1; count < 5; count++) System.out.println( “Count” + count” ); sum = sum + count; System.out.println( “Sum: ” + sum ); } How does this behave? What have we forgotten?

23 Exercise How would you write a function to print out the powers of 3 less than n? e.g. For 1000: 1 3 9 27 81 243 729


Download ppt "Repetition Statements"

Similar presentations


Ads by Google