Chapter 7 Repetition Statements 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 7-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 1 Chapter 7 Repetition Statements
Chapter 7 objectives After you have read and studied this chapter, you should be able to e Implement repetition control in a program using while statements e Implement repetition control in a program using do-while statements e Implement repetition control in a program using for statements e Nest a loop repetition statement inside another repetition statement Choose the appropriate repetition control statement for a given task e Prompt the user for a yes-no reply using the ResponseBox class from the javabook package e Output formatted data using the Format class from the javabook package. (Optional) Write simple recursive methods C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 2 Chapter 7 Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program using while statements. Implement repetition control in a program using do–while statements. Implement repetition control in a program using for statements. Nest a loop repetition statement inside another repetition statement. Choose the appropriate repetition control statement for a given task. Prompt the user for a yes–no reply using the ResponseBox class from the javabook package. Output formatted data using the Format class from the javabook package. (Optional) Write simple recursive methods
The while statement int sum =0, number while( number <=100) Sum sum number; These statements are executed as long as number is less than or number= number 1 equal to 100 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 3 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
Syntax for the while statement while( <boolean expression> <statement> Boolean Expression while(: number < 100 ··················································· 。sum sum number Statement (loop body) ∵ number number 1 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 4 while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( <boolean expression> ) <statement> Statement (loop body) Boolean Expression
Control flow of while int sum =0. number= 1 number < true 100? false sum sum number number number 1 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 5 Control Flow of while int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; true