《JAVA OOP开发》英文版 Chapter 7 Repetition Statements

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 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 ) 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

More Examples int sum Keeps adding the bers.2.3 .. until while( sum <=1000000)( the sum becomes larger than1.000.000 sum sum number umb umber 1 2 int product 1, numbe count 20, lastNumberi Computes the product of the first 20 odd integers lastNumber =2 while(number < lastNumber)( product product numberi number 2; C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 6 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

Example: Testing Input data lere's a, realistic example of using the while loop to accept only he va Input data r Accepts age between 0 and 130, exclusively Priming Read age inputBox getInteger("Your Age (between 0 and 130): hile(age 130) messageBox. show("An invalid age was entered.+ Please try again. age inputBox getInteger(" Your Age (between 0 and 130): )i C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 7 age = inputBox.getInteger("Your Age (between 0 and 130):"); while (age 130) { messageBox.show("An invalid age was entered. " + "Please try again."); age = inputBox.getInteger( "Your Age (between 0 and 130):" ); } Example: Testing Input Data Here’s a realistic example of using the while loop to accept only the valid input data. Accepts age between 0 and 130, exclusively. Priming Read

while Loop pitfall -1 int product =0 while( product 500000)i product= product 5i Infinite Loops Both loops will not terminate because the 2) int count =1 boolean expressions will never become false while( count count count 2 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 8 while 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

while Loop pitfall -2 float count =0. 0f while( count ! 1 0f ) count count +0.3333333f //seven 3s Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real 2) float count =0.0f number can be stored in a computer memory while( count ! 1 0f ) count count +0.33333333f //eight 3s C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 9 while 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 computer memory. float count = 0.0f; while ( count != 1.0f ) { count = count + 0.33333333f; } //eight 3s 2 float count = 0.0f; while ( count != 1.0f ) { count = count + 0.3333333f; } //seven 3s 1

while Loop pitfall -3 r Goal: Execute the loop body 10 times ① count 1 (2 count 1 while( count 10) while( count <= 10) count++ count++i 3 count o 4 count=o while( count < 10 while( count 10) count++i coun七++; 1 and (3) exhibit off-by-one error C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 10 while Loop Pitfall - 3 Goal: Execute the loop body 10 times. count = 1; while ( count < 10 ) { . . . count++; } 1 count = 0; while ( count <= 10 ) { . . . count++; } 3 count = 1; while ( count <= 10 ) { . . . count++; } 2 count = 0; while ( count < 10 ) { . . . count++; } 4 1 and 3 exhibit off-by-one error
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第二讲 数据类型与算术运算.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第九讲 函数.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第九章 函数.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第三章 运算符与表达式.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt