ImageVerifierCode 换一换
格式:PPT , 页数:58 ,大小:275KB ,
资源ID:379726      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-379726.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Chapter 3- Program Statements.ppt)为本站会员(eveningprove235)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Chapter 3- Program Statements.ppt

1、Chapter 3: Program Statements,Presentation slides forJava Software Solutions Foundations of Program Design Second Editionby John Lewis and William LoftusJava Software Solutions is published by Addison-WesleyPresentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved.

2、 Instructors using the textbook may use and modify these slides for pedagogical purposes.,2,Program Statements,We will now examine some other program statementsChapter 3 focuses on: the flow of control through a method decision-making statements operators for making complex decisions repetition stat

3、ements software development stages more drawing techniques,Flow of Control,Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are writtenSome programming statements modify that order, allowing us to: decide whether or not to

4、 execute a particular statement, or perform a statement over and over repetitivelyThe order of statement execution is called the flow of control,Conditional Statements,A conditional statement lets us choose which statement will be executed nextTherefore they are sometimes called selection statements

5、Conditional statements give us the power to make basic decisionsJavas conditional statements are the if statement, the if-else statement, and the switch statement,5,The if Statement,The if statement has the following syntax:,if ( condition )statement;,The if Statement,An example of an if statement:,

6、if (sum MAX)delta = sum - MAX; System.out.println (“The sum is “ + sum);,First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not.,If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped.,Eithe

7、r way, the call to println is executed next.,See Age.java (page 112),Logic of an if statement,false,8,Boolean Expressions,A condition often uses one of Javas equality operators or relational operators, which all return boolean results:= equal to != not equal togreater than = greater than or equal to

8、Note the difference between the equality operator (=) and the assignment operator (=),9,The if-else Statement,An else clause can be added to an if statement to make it an if-else statement:,if ( condition )statement1; elsestatement2;,See Wages.java (page 116),If the condition is true, statement1 is

9、executed; if the condition is false, statement2 is executed,One or the other will be executed, but not both,Logic of an if-else statement,11,Block Statements,Several statements can be grouped together into a block statementA block is delimited by braces ( )A block statement can be used wherever a st

10、atement is called for in the Java syntaxFor example, in an if-else statement, the if portion, or the else portion, or both, could be block statementsSee Guessing.java (page 117),12,Nested if Statements,The statement executed as a result of an if statement or else clause could be another if statement

11、These are called nested if statementsSee MinOfThree.java (page 118)An else clause is matched to the last unmatched if (no matter what the indentation implies),Comparing Characters,We can use the relational operators on character data The results are based on the Unicode character set The following c

12、ondition is true because the character + comes before the character J in Unicode:,if (+ J)System.out.println (“+ is less than J“);,The uppercase alphabet (A-Z) and the lowercase alphabet (a-z) both appear in alphabetical order in Unicode,Comparing Strings,Remember that a character string in Java is

13、an objectWe cannot use the relational operators to compare stringsThe equals method can be called on a string to determine if two strings contain exactly the same characters in the same orderThe String class also contains a method called compareTo to determine if one string comes before another alph

14、abetically (as determined by the Unicode character set),Comparing Floating Point Values,We also have to be careful when comparing two floating point values (float or double) for equality You should rarely use the equality operator (=) when comparing two floats In many situations, you might consider

15、two floating point numbers to be “close enough“ even if they arent exactly equal Therefore, to determine the equality of two floats, you may want to use the following technique:,if (Math.abs (f1 - f2) 0.00001)System.out.println (“Essentially equal.“);,16,The switch Statement,The switch statement pro

16、vides another means to decide which statement to execute nextThe switch statement evaluates an expression, then attempts to match the result to one of several possible casesEach case contains a value and a list of statementsThe flow of control transfers to statement list associated with the first va

17、lue that matches,The switch Statement,The general syntax of a switch statement is:,switch ( expression ) case value1 :statement-list1case value2 :statement-list2case value3 :statement-list3case .,If expression matches value2, control jumps to here,The switch Statement,Often a break statement is used

18、 as the last statement in each cases statement listA break statement causes control to transfer to the end of the switch statementIf a break statement is not used, the flow of control will continue into the next caseSometimes this can be helpful, but usually we only want to execute the statements as

19、sociated with one case,The switch Statement,A switch statement can have an optional default caseThe default case has no associated value and simply uses the reserved word defaultIf the default case is present, control will transfer to it if no other case value matchesThough the default case can be p

20、ositioned anywhere in the switch, it is usually placed at the endIf there is no default case, and no other value matches, control falls through to the statement after the switch,The switch Statement,The expression of a switch statement must result in an integral data type, like an integer or charact

21、er; it cannot be a floating point valueNote that the implicit boolean condition in a switch statement is equality - it tries to match the expression with a valueYou cannot perform relational checks with a switch statementSee GradeReport.java (page 121),21,Logical Operators,Boolean expressions can al

22、so use the following logical operators:! Logical NOT& Logical AND| Logical ORThey all take boolean operands and produce boolean resultsLogical NOT is a unary operator (it has one operand), but logical AND and logical OR are binary operators (they each have two operands),22,Logical NOT,The logical NO

23、T operation is also called logical negation or logical complementIf some boolean condition a is true, then !a is false; if a is false, then !a is trueLogical expressions can be shown using truth tables,23,Logical AND and Logical OR,The logical and expressiona & bis true if both a and b are true, and

24、 false otherwiseThe logical or expressiona | bis true if a or b or both are true, and false otherwise,Truth Tables,A truth table shows the possible true/false combinations of the terms Since & and | each have two operands, there are four possible combinations of true and false,25,Logical Operators,C

25、onditions in selection statements and loops can use logical operators to form complex expressions,if (total MAX ,Logical operators have precedence relationships between themselves and other operators,26,Truth Tables,Specific expressions can be evaluated using truth tables,27,More Operators,To round

26、out our knowledge of Java operators, lets examine a few moreIn particular, we will examine the:increment and decrement operators assignment operators conditional operator,28,Increment and Decrement Operators,The increment and decrement operators are arithmetic and operate on one operand The incremen

27、t operator (+) adds one to its operand The decrement operator (-) subtracts one from its operand The statementcount+;is essentially equivalent tocount = count + 1;,29,Increment and Decrement Operators,The increment and decrement operators can be applied in prefix form (before the variable) or postfi

28、x form (after the variable)When used alone in a statement, the prefix and postfix forms are basically equivalent. That is,count+;is equivalent to+count;,30,Increment and Decrement Operators,When used in a larger expression, the prefix and postfix forms have a different effect In both cases the varia

29、ble is incremented (decremented) But the value used in the larger expression depends on the form:,31,Increment and Decrement Operators,If count currently contains 45, thentotal = count+;assigns 45 to total and 46 to countIf count currently contains 45, thentotal = +count;assigns the value 46 to both

30、 total and count,32,Assignment Operators,Often we perform an operation on a variable, then store the result back into that variable Java provides assignment operators to simplify that process For example, the statementnum += count;is equivalent tonum = num + count;,33,Assignment Operators,There are

31、many assignment operators, including the following:,34,Assignment Operators,The right hand side of an assignment operator can be a complete expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Thereforeresult /= (total-MIN) % num;is e

32、quivalent toresult = result / (total-MIN) % num);,35,The Conditional Operator,Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluatedThe result of the chosen expression is the result of the entire conditional operatorIts syntax i

33、s:condition ? expression1 : expression2If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated,36,The Conditional Operator,The conditional operator is similar to an if-else statement, except that it is an expression that returns a valueFor example:larger = (num1

34、num2) ? num1 : num2;If num1 is greater that num2, then num1 is assigned to larger; otherwise, num2 is assigned to largerThe conditional operator is ternary, meaning that it requires three operands,37,The Conditional Operator,Another example:System.out.println (“Your change is “ + count +(count = 1)

35、? “Dime“ : “Dimes“);If count equals 1, then “Dime“ is printed If count is anything other than 1, then “Dimes“ is printed,Repetition Statements,Repetition statements allow us to execute a statement multiple times repetitivelyThey are often simply referred to as loopsLike conditional statements, they

36、are controlled by boolean expressionsJava has three kinds of repetition statements: the while loop, the do loop, and the for loopThe programmer must choose the right kind of loop for the situation,39,The while Statement,The while statement has the following syntax:,while ( condition )statement;,The

37、statement is executed repetitively until the condition becomes false.,Logic of a while loop,false,41,The while Statement,Note that if the condition of a while statement is false initially, the statement is never executedTherefore, the body of a while loop will execute zero or more timesSee Counter.j

38、ava (page 133)See Average.java (page 134)See WinPercentage.java (page 136),42,Infinite Loops,The body of a while loop must eventually make the condition falseIf not, it is an infinite loop, which will execute until the user interrupts the programSee Forever.java (page 138) This is a common type of l

39、ogical errorYou should always double check to ensure that your loops will terminate normally,Nested Loops,Similar to nested if statements, loops can be nested as wellThat is, the body of a loop could contain another loopEach time through the outer loop, the inner loop will go through its entire set

40、of iterationsSee PalindromeTester.java (page 137),The do Statement,The do statement has the following syntax:,do statement; while ( condition ),The statement is executed once initially, then the condition is evaluated,The statement is repetitively executed until the condition becomes false,Logic of

41、a do loop,true,false,The do Statement,A do loop is similar to a while loop, except that the condition is evaluated after the body of the loop is executedTherefore the body of a do loop will execute at least one timeSee Counter2.java (page 143)See ReverseNumber.java (page 144),Comparing the while and

42、 do loops,The for Statement,The for statement has the following syntax:,for ( initialization ; condition ; increment )statement;,The for Statement,A for loop is equivalent to the following while loop structure:,initialization; while ( condition ) statement;increment; ,Logic of a for loop,false,The f

43、or Statement,Like a while loop, the condition of a for statement is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times It is well suited for executing a specific number of times that can be determined in advanceSee Counter3.java (page 146)See Mu

44、ltiples.java (page 147)See Stars.java (page 150),The for Statement,Each expression in the header of a for loop is optionalIf the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the

45、increment is left out, no increment operation is performedBoth semi-colons are always required in the for loop header,53,Program Development,The creation of software involves four basic activities:establishing the requirements creating a design implementing the code testing the implementationThe dev

46、elopment process is much more involved than this, but these basic steps are a good starting point,54,Requirements,Requirements specify the tasks a program must accomplish (what to do, not how to do it) They often include a description of the user interface An initial set of requirements are often pr

47、ovided, but usually must be critiqued, modified, and expanded It is often difficult to establish detailed, unambiguous, complete requirements Careful attention to the requirements can save significant time and money in the overall project,55,Design,An algorithm is a step-by-step process for solving

48、a problem A program follows one or more algorithms to accomplish its goal The design of a program specifies the algorithms and data needed In object-oriented development, the design establishes the classes, objects, and methods that are required The details of a method may be expressed in pseudocode

49、, which is code-like, but does not necessarily follow any specific syntax,56,Implementation,Implementation is the process of translating a design into source code Most novice programmers think that writing code is the heart of software development, but it actually should be the least creative step A

50、lmost all important decisions are made during requirements analysis and design Implementation should focus on coding details, including style guidelines and documentationSee ExamGrades.java (page 155),57,Testing,A program should be executed multiple times with various input in an attempt to find errors Debugging is the process of discovering the cause of a problem and fixing it Programmers often erroneously think that there is “only one more bug“ to fix Tests should focus on design details as well as overall requirements,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1