Chapter 3- Program Statements.ppt
《Chapter 3- Program Statements.ppt》由会员分享,可在线阅读,更多相关《Chapter 3- Program Statements.ppt(58页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER3PROGRAMSTATEMENTSPPT
