Week2 - WEEK 2 Repetition Statements & Arrays...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: WEEK 2 Repetition Statements & Arrays CS 180 Prof. Sunil Prabhakar Department of Computer Science Purdue University Review Looping constructs: while, do-while, for nested looping statements Arrays Declare and use arrays Arrays as arguments and return values Multi-dimensional arrays. 3 while ( number <= 100 ) { sum = sum + number; number = number + 1; } The while Statement while ( <boolean expression> ) <statement> 4 Control Flow of while next statement; true false sum = sum + number; number = number + 1; int sum = 0, number = 1 previous statement; number <= 100 ? while ( number <= 100 ) { sum = sum + number; number = number + 1; } 5 More Examples Keeps adding the numbers 1, 2, 3, until the sum becomes larger than 1,000,000. Computes the product of the first 20 odd integers. int sum = 0, number = 1; while ( sum <= 1000000 ) { sum = sum + number; number = number + 1; } 1 int product = 1, number = 1, count = 20, lastNumber; lastNumber = 2 * count - 1; while ( number <= lastNumber ) { product = product * number; number = number + 2; } 2 6 String inputStr; int age; inputStr = JOptionPane.showInputDialog ( null , "Your Age (between 0 and 130):" ) ; age = Integer.parseInt ( inputStr ) ; while ( age < 0 || age > 130 ) { JOptionPane.showMessageDialog ( null , "An invalid age was entered. Please try again." ) ; inputStr = JOptionPane.showInputDialog ( null , "Your Age (between 0 and 130):" ) ; age = Integer.parseInt ( inputStr ) ; } Example: Testing Input Data Priming Read 7 Watch Out for Pitfalls 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. 4. If you want to execute the loop body N times, then initialize the counter to and use the test condition counter < N or initialize the counter to 1 and use the test condition counter <= N . Loop Pitfall - 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 int product = 0; while ( product < 500000 ) { product = product * 5; } 1 9 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 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. 10 Loop Pitfall - 2 Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computers memory....
View Full Document

Page1 / 71

Week2 - WEEK 2 Repetition Statements & Arrays...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online