Kiến trúc máy tính và hợp ngữ - Chương 7: Java methods
Can you have an if statement without an else?
What are De Morgan’s Laws?
Explain short-circuit evaluation.
How long can an if-else-if sequence be?
30 trang |
Chia sẻ: huyhoang44 | Lượt xem: 791 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Kiến trúc máy tính và hợp ngữ - Chương 7: Java methods, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Boolean Expressions and if-else Statements if (chapter == 7)Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.Java MethodsObject-Oriented Programmingand Data StructuresMaria Litvin ● Gary Litvin2nd AP edition with GridWorld1Objectives:Learn about the boolean data typeLearn the syntax for if-else statementsLearn about relational and logical operators, De Morgan’s laws, short-circuit evaluationLearn when to use nested if-else statements, if-else-if sequences, the switch statementLearn about enum data types2if-else Statementif ( ){ }else{ }if ( ){ }else clause is optional3boolean Data TypeGeorge Boole (1815 - 1864)boolean variables may have only two values, true or false.You define boolean fields or boolean local variables the same way as other variables.boolean truefalseReserved wordsprivate boolean hasMiddleName;boolean isRolling = false;4Boolean ExpressionsIn if ( ) is a Boolean expression.A Boolean expression evaluates to either true or false.Boolean expressions are written using boolean variables and relational and logical operators.5Relational Operators , =, ==, !=is equal tois NOT equal to6Relational Operators (cont’d)Apply to numbers or chars: if ( count1 -10 && x 10 ) ... ! (p && q) == ( !p || !q ) ! (p || q) == ( !p && !q )Easier to read13Ranks of Operators! -(unary) ++ -- (cast)* / %+ - >= == != &&|| if ( ( ( year % 4 ) == 0 ) && ( month == 2 ) ) ... if ( year % 4 == 0 && month == 2 ) ...Easier to readHighestLowest14Short-Circuit Evaluationif (condition1 && condition2) ... If condition1 is false, then condition2 is not evaluated (the result is false anyway)if (condition1 || condition2) ... If condition1 is true, then condition2 is not evaluated (the result is true anyway)if ( x >= 0 && Math.sqrt (x) = numSlides) beep.play(); else slide++;}else{ if (slide <= 1) beep.play(); else slide--;}16if-else-ifif (drinkSize.equals(”Large")){ total += 1.39;}else if (drinkSize.equals("Medium")){ total += 1.19;}else // if "Small" drink size{ total += 0.99;}17Common if-else Errorsif (...) if (...) statement1;else statement2;...It is safer to always use braces in if-elseif (...) statement1; statement2;...if (...) ;{ statements; ...}Extra semicolon:Missing braces:18The switchStatementswitch (expression){ case value1: ... break; case value2: ... break; ... ... default: ... break;}Don’t forget breaks!switch casedefaultbreakReserved words19The switch Statement (cont’d)The same case can have two or more labels. For example:switch (num){ case 1: case 2: System.out.println ("Buckle your shoe"); break; case 3: ...}20enum Data TypesUsed when an object’s attribute or state can have only one of a small set of values, for example:enum variables do not represent numbers, characters, or strings. private enum Speed { LOW, MEDIUM, HIGH }; private enum InkColor { BLACK, RED }; private enum DayOfWeek { sunday, monday, tuesday, wednesday, thursday, friday, saturday }; 21enum Data Types (cont’d)Use == or != to compare enum valuesCan be used in a switch: private enum Speed { LOW, MEDIUM, HIGH }; ... Speed currentSpeed = Speed.LOW; ... if (currentSpeed == Speed.LOW) ... switch (currentSpeed) { case LOW: ... break; case MEDIUM: ...22The Craps Project23The Craps Project (cont’d)Your job24The Craps Project (cont’d)Step 1:Test your CrapsGame class separately using the class CrapsTest1 provided to you.CrapsTest1CrapsGame25The Craps Project (cont’d)Step 2:Use your Die and CrapsGame classes in the CrapsStats application.CrapsStatsCrapsGameDie26The Craps Project (cont’d)Step 3:Finish the RollingDie class (the drawDots method). Integrate CrapsGame and RollingDie into the Craps program.27Review:What are the possible values of a boolean variable?What operators are used to compare values of numbers and chars?How can you test whether two Strings have the same values? Which binary operators have higher rank (are executed first), relational or logical?28Review (cont’d):Can you have an if statement without an else?What are De Morgan’s Laws?Explain short-circuit evaluation.How long can an if-else-if sequence be?29Review (cont’d):What are breaks used for in a switch? What happens if you forget one?Can the same case in a switch have two breaks?What types of expressions can be used in a switch? chars? ints? doubles? enums?What operators can have enum operands?30
Các file đính kèm theo tài liệu này:
- ch07_2674.ppt