java语言程序设计-基础篇课件(第4章)英文.ppt

上传人(卖家):晟晟文业 文档编号:4301062 上传时间:2022-11-27 格式:PPT 页数:48 大小:213KB
下载 相关 举报
java语言程序设计-基础篇课件(第4章)英文.ppt_第1页
第1页 / 共48页
java语言程序设计-基础篇课件(第4章)英文.ppt_第2页
第2页 / 共48页
java语言程序设计-基础篇课件(第4章)英文.ppt_第3页
第3页 / 共48页
java语言程序设计-基础篇课件(第4章)英文.ppt_第4页
第4页 / 共48页
java语言程序设计-基础篇课件(第4章)英文.ppt_第5页
第5页 / 共48页
点击查看更多>>
资源描述

1、Chapter 4 Loops1MotivationsSuppose that you need to print a string(e.g.,Welcome to Java!)a hundred times.It would be tedious to have to write the following statement a hundred times:System.out.println(Welcome to Java!);So,how do you solve this problem?2Opening ProblemProblem:3System.out.println(Welc

2、ome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome to Java!);System.out.println(Welcome

3、to Java!);100 timesIntroducing while Loops4int count=0;while(count 100)System.out.println(Welcome to Java);count+;Objectives5FTo write programs for executing statements repeatedly using a while loop(4.2).FTo develop a program for GuessNumber and SubtractionQuizLoop(4.2.1).FTo follow the loop design

4、strategy to develop loops(4.2.2).FTo develop a program for SubtractionQuizLoop(4.2.3).FTo control a loop with a sentinel value(4.2.3).FTo obtain large input from a file using input redirection rather than typing from the keyboard(4.2.4).FTo write loops using do-while statements(4.3).FTo write loops

5、using for statements(4.4).FTo discover the similarities and differences of three types of loop statements(4.5).FTo write nested loops(4.6).FTo learn the techniques for minimizing numerical errors(4.7).FTo learn loops from a variety of examples(GCD,FutureTuition,MonteCarloSimulation)(4.8).FTo impleme

6、nt program control with break and continue(4.9).F(GUI)To control a loop with a confirmation dialog(4.10).while Loop Flow Chart6while(loop-continuation-condition)/loop-body;Statement(s);int count=0;while(count 100)System.out.println(Welcome to Java!);count+;Loop Continuation Condition?true Statement(

7、s)(loop body)false (count 100)?true System.out.println(Welcome to Java!);count+;false(A)(B)count=0;Trace while Loop7int count=0;while(count 2)System.out.println(Welcome to Java!);count+;Initialize countanimationTrace while Loop,cont.8int count=0;while(count 2)System.out.println(Welcome to Java!);cou

8、nt+;(count 2)is trueanimationTrace while Loop,cont.9int count=0;while(count 2)System.out.println(Welcome to Java!);count+;Print Welcome to JavaanimationTrace while Loop,cont.10int count=0;while(count 2)System.out.println(Welcome to Java!);count+;Increase count by 1count is 1 nowanimationTrace while

9、Loop,cont.11int count=0;while(count 2)System.out.println(Welcome to Java!);count+;(count 2)is still true since count is 1animationTrace while Loop,cont.12int count=0;while(count 2)System.out.println(Welcome to Java!);count+;Print Welcome to JavaanimationTrace while Loop,cont.13int count=0;while(coun

10、t 2)System.out.println(Welcome to Java!);count+;Increase count by 1count is 2 nowanimationTrace while Loop,cont.14int count=0;while(count 2)System.out.println(Welcome to Java!);count+;(count 2)is false since count is 2 nowanimationTrace while Loop15int count=0;while(count 2)System.out.println(Welcom

11、e to Java!);count+;The loop exits.Execute the next statement after the loop.animationProblem:Guessing Numbers Write a program that randomly generates an integer between 0 and 100,inclusive.The program prompts the user to enter a number continuously until the number matches the randomly generated num

12、ber.For each user input,the program tells the user whether the input is too low or too high,so the user can choose the next input intelligently.Here is a sample run:16RunRunProblem:An Advanced Math Learning Tool The Math subtraction learning tool program generates just one question for each run.You

13、can use a loop to generate questions repeatedly.This example gives a program that generates five questions and reports the number of the correct answers after a student answers all five questions.17RunEnding a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermin

14、ed.You may use an input value to signify the end of the loop.Such a value is known as a sentinel value.Write a program that reads and calculates the sum of an unspecified number of integers.The input 0 signifies the end of the input.18RunCautionDont use floating-point values for equality checking in

15、 a loop control.Since floating-point values are approximations for some values,using them could result in imprecise counter values and inaccurate results.Consider the following code for computing 1+0.9+0.8+.+0.1:double item=1;double sum=0;while(item!=0)/No guarantee item will be 0 sum+=item;item-=0.

16、1;System.out.println(sum);Variable item starts with 1 and is reduced by 0.1 every time the loop body is executed.The loop should terminate when item becomes 0.However,there is no guarantee that item will be exactly 0,because the floating-point arithmetic is approximated.This loop seems OK on the sur

17、face,but it is actually an infinite loop.19do-while Loop20do /Loop body;Statement(s);while(loop-continuation-condition);Loop Continuation Condition?true Statement(s)(loop body)false for Loopsfor(initial-action;loop-continuation-condition;action-after-each-iteration)/loop body;Statement(s);21int i;fo

18、r(i=0;i 100;i+)System.out.println(Welcome to Java!);Loop Continuation Condition?true Statement(s)(loop body)false(A)Action-After-Each-Iteration Initial-Action (i 100)?true System.out.println(Welcome to Java);false(B)i+i=0 Trace for Loop22int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Decl

19、are ianimationTrace for Loop,cont.23int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Execute initializeri is now 0animationTrace for Loop,cont.24int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);(i 2)is true since i is 0animationTrace for Loop,cont.25int i;for(i=0;i 2;i+)System.out.

20、println(Welcome to Java!);Print Welcome to JavaanimationTrace for Loop,cont.26int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Execute adjustment statement i now is 1animationTrace for Loop,cont.27int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);(i 2)is still true since i is 1anima

21、tionTrace for Loop,cont.28int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Print Welcome to JavaanimationTrace for Loop,cont.29int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Execute adjustment statement i now is 2animationTrace for Loop,cont.30int i;for(i=0;i 2;i+)System.out.prin

22、tln(Welcome to Java!);(i 2)is false since i is 2animationTrace for Loop,cont.31int i;for(i=0;i 2;i+)System.out.println(Welcome to Java!);Exit the loop.Execute the next statement after the loopanimationNote32The initial-action in a for loop can be a list of zero or more comma-separated expressions.Th

23、e action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements.Therefore,the following two for loops are correct.They are rarely used in practice,however.for(int i=1;i 100;System.out.println(i+);for(int i=0,j=0;(i+j 10);i+,j+)/Do something Note33If the loop-cont

24、inuation-condition in a for loop is omitted,it is implicitly true.Thus the statement given below in(a),which is an infinite loop,is correct.Nevertheless,it is better to use the equivalent loop in(b)to avoid confusion:for(;)/Do something (a)Equivalent while(true)/Do something (b)CautionAdding a semic

25、olon at the end of the for clause before the loop body is a common mistake,as shown below:34Logic Errorfor(int i=0;i10;i+);System.out.println(i is +i);Caution,cont.Similarly,the following loop is also wrong:int i=0;while(i 10);System.out.println(i is +i);i+;In the case of the do loop,the following s

26、emicolon is needed to end the loop.int i=0;do System.out.println(i is +i);i+;while(i10);35Logic ErrorCorrectWhich Loop to Use?36The three forms of loop statements,while,do-while,and for,are expressively equivalent;that is,you can write a loop in any of these three forms.For example,a while loop in(a

27、)in the following figure can always be converted into the following for loop in(b):A for loop in(a)in the following figure can generally be converted into the following while loop in(b)except in certain special cases(see Review Question 3.19 for one of them):for(initial-action;loop-continuation-cond

28、ition;action-after-each-iteration)/Loop body;(a)Equivalent(b)initial-action;while(loop-continuation-condition)/Loop body;action-after-each-iteration;while(loop-continuation-condition)/Loop body (a)Equivalent(b)for(;loop-continuation-condition;)/Loop body Recommendations37Use the one that is most int

29、uitive and comfortable for you.In general,a for loop may be used if the number of repetitions is known,as,for example,when you need to print a message 100 times.A while loop may be used if the number of repetitions is not known,as in the case of reading the numbers until the input is 0.A do-while lo

30、op can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.Nested Loops Problem:Write a program that uses nested for loops to print a multiplication table.38RunMinimizing Numerical Errors Numeric errors involving floating-point numbers are in

31、evitable.This section discusses how to minimize such errors through an example.Here is an example that sums a series that starts with 0.01 and ends with 1.0.The numbers in the series will increment by 0.01,as follows:0.01+0.02+0.03 and so on.39RunProblem:Finding the Greatest Common Divisor 40Problem

32、:Write a program that prompts the user to enter two positive integers and finds their greatest common divisor.Solution:Suppose you enter two integers 4 and 2,their greatest common divisor is 2.Suppose you enter two integers 16 and 24,their greatest common divisor is 8.So,how do you find the greatest

33、 common divisor?Let the two input integers be n1 and n2.You know number 1 is a common divisor,but it may not be the greatest commons divisor.So you can check whether k(for k=2,3,4,and so on)is a common divisor for n1 and n2,until k is greater than n1 or n2.Problem:Predicating the Future Tuition 41Pr

34、oblem:Suppose that the tuition for a university is$10,000 this year and tuition increases 7%every year.In how many years will the tuition be doubled?Problem:Predicating the Future Tuition 42double tuition=10000;int year=1 /Year 1tuition=tuition*1.07;year+;/Year 2tuition=tuition*1.07;year+;/Year 3tui

35、tion=tuition*1.07;year+;/Year 4.Problem:Monte Carlo Simulation 43The Monte Carlo simulation refers to a technique that uses random numbers and probability to solve problems.This method has a wide range of applications in computational mathematics,physics,chemistry,and finance.This section gives an e

36、xample of using the Monto Carlo simulation for estimating.circleArea/squareArea=/4.can be approximated as 4*numberOfHits/1000000.Using break and continue44Examples for using the break and continue keywords:FTestBreak.javaFTestContinue.javaRunRunGuessing Number Problem Revisited Here is a program for

37、 guessing a number.You can rewrite it using a break statement.45RunProblem:Displaying Prime Numbers46Problem:Write a program that displays the first 50 prime numbers in five lines,each of which contains 10 numbers.An integer greater than 1 is prime if its only positive divisor is 1 or itself.For exa

38、mple,2,3,5,and 7 are prime numbers,but 4,6,8,and 9 are not.Solution:The problem can be broken into the following tasks:For number=2,3,4,5,6,.,test whether the number is prime.Determine whether a given number is prime.Count the prime numbers.Print each prime number,and print 10 numbers per line.(GUI)

39、Controlling a Loop with a Confirmation Dialog 47A sentinel-controlled loop can be implemented using a confirmation dialog.The answers Yes or No to continue or terminate the loop.The template of the loop may look as follows:int option=0;while(option=JOptionPane.YES_OPTION)System.out.println(continue loop);option=JOptionPane.showConfirmDialog(null,Continue?);Debugging Loops in IDE Tools48Companion WebsiteSupplements II.C,II.E,and II.G.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(java语言程序设计-基础篇课件(第4章)英文.ppt)为本站会员(晟晟文业)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|