Chapter 6 - DecisionsCharactersPracticeChapter GoalsControlBooleanRelational OperatorsBoolean Operatorsif statementSyntaxif statementsBankAccount RevistedVersion 2.0Slide 14Simple vs. CompoundSlide 16Productivity Hint 6.1If statementDefinitionSyntax – if statementComparing integersComparing Floating PointRoundoffSlide 24if StatementExample SetupSimple if StatementControl Flow of If statementSimple if-else StatementControl Flow of if-elseAdvanced Topic6.3 Multiple AlternativesMultiple alternativesWRONG versionElse matters alsoAdv 6.2 Switch StatementSlide 37Slide 38Slide 39Switch statement6.3.2 Nested branchesExampleTax ScheduleSlide 44Slide 45Slide 46Slide 47Dangling elseSlide 49Boolean operatorsTruth tableSlide 52Slide 53Precedence TableLazy EvaluationCommon UsesStyle6.2.3 Comparing StringsString equalityequalsIgnoreCase()compareTo()6.2.4 Comparing ObjectsSlide 636.2.4 Testing for nullSlide 65Draw the Memory DiagramEqualitySlide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74StringString Concatenation6.4 Using Boolean Expressions6.4.2 Predicate methodsPredicate MethodsSlide 80WeightBetter SolutionSlide 83Chapter 6 - Chapter 6 - DecisionsDecisionsCharactersCharacterscharchar primitive data typeprimitive data typestores a single characterstores a single characteroperations operations (same as (same as intint)):: + -+ -special chars special chars (use backslash)(use backslash)::''\\nn'' ''\\tt'' ''\\\\'' ''\\''''ExampleExample char c = char c = ''aa'';;char q = char q = ''\\''''; // stores a single quote ; // stores a single quote caq'PracticePracticeint i = (int) 'D'char c = (char) 106i = 'D' + 'A'c = (char)('D' + 42)68j133‘n’Chapter GoalsChapter GoalsTo be able to implement decisions using if To be able to implement decisions using if statements statements To understand how to group statements To understand how to group statements into blocks into blocks To learn how to compare integers, To learn how to compare integers, floating-point numbers, strings, and floating-point numbers, strings, and objects objects To recognize the correct ordering of To recognize the correct ordering of decisions in multiple branches decisions in multiple branches To program conditions using Boolean To program conditions using Boolean operators and variables operators and variablesControlControlThe real world requires decisionsThe real world requires decisionsIs it time to feed the cat? Can a book be Is it time to feed the cat? Can a book be checked out?checked out?Essential feature of nontrivial programs Essential feature of nontrivial programs is to make decisions based on inputis to make decisions based on inputThe answers to these decision The answers to these decision require different actionsrequire different actionsIntroduces the need for Introduces the need for control control statementsstatementsBooleanBooleanbooleanboolean is a primitive data type that is a primitive data type that holds one of two valuesholds one of two valuestruetruefalsefalseAny time a question needs to be Any time a question needs to be asked, we use a boolean expression, asked, we use a boolean expression, which returns a boolean valuewhich returns a boolean valueAs apposed to a mathematical expression As apposed to a mathematical expression which returns a number valuewhich returns a number valueRelational OperatorsRelational OperatorsWhat operators are used for boolean What operators are used for boolean expressions?expressions?<<less thanless than<=<=less than or equal toless than or equal to>>greater thangreater than>=>=greater than or equal togreater than or equal to====equal toequal to!=!=not equal tonot equal toBoolean OperatorsBoolean OperatorsBoolean expressions are just like Boolean expressions are just like mathematical expressionsmathematical expressionsBut here, they return a true or false But here, they return a true or false (not int, double, etc)(not int, double, etc)These are the 6 binary operatorsThese are the 6 binary operatorsEx.Ex.testScore < 80testScore < 80testScore * 2 > 350testScore * 2 > 350if statementif statementConditional statements are Conditional statements are represented with if-else statementsrepresented with if-else statementsScanner stdin = new Scanner(System.in);Scanner stdin = new Scanner(System.in);int testScore = stdin.nextInt();int testScore = stdin.nextInt();ifif (testScore < 70){ (testScore < 70){System.out.println(“You are below the mean”);System.out.println(“You are below the mean”);}}else {else {System.out.println(“You are above the mean”);System.out.println(“You are above the mean”);}}SyntaxSyntaxifif (<boolean expression>){ (<boolean expression>){<then block><then block>}}else{else{<else block><else block>}}if statementsif statementsWhen the code reaches the if statement, When the code reaches the if statement, it evaluates the boolean expressionit evaluates the boolean expressionIf it is true, the <then block> is executedIf it is true, the <then block> is executedIf it is false, the <else block> is executedIf it is false, the <else block> is executedCalled a Called a branching statementbranching statement because it because it branches to a block of codebranches to a block of codeBankAccountBankAccount Revisted RevistedWithdraw method we implemented Withdraw method we implemented allowed user to withdraw as much allowed user to withdraw as much money as they wantedmoney as they wantedIs this realistic?Is this realistic?What What decisiondecision should should withdraw()withdraw() make? make?What should the decision be made on?What should the decision be made on?Version 2.0Version 2.0public void withdraw(double amount){public void withdraw(double amount){if (amount <= balance)if (amount <= balance)balance -= amount;balance -= amount;}}Simple vs. CompoundSimple vs. CompoundThe decision we have made only The decision we have made only resulted in one instructionresulted in one instructionMost decisions lead to a path of Most decisions lead to a path of instructions, requiring multiple instructions, requiring multiple statements. The statements are statements. The statements are grouped with curly braces{ } grouped with curly braces{ } if (amount <= balance) {if (amount <= balance) {System.out.println(“Legal Withdrawal”);System.out.println(“Legal Withdrawal”);balance = balance – amount;balance = balance –
View Full Document