DOC PREVIEW
UW-Madison CS 302 - Decisions

This preview shows page 1-2-3-4-5-6-38-39-40-41-42-78-79-80-81-82-83 out of 83 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 83 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 - DecisionsDecisionsCharactersCharacterscharchar primitive data typeprimitive data typestores a single characterstores a single characteroperations 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 GoalsTo 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 variablesControlControlThe real world requires decisionsThe real world requires decisionsIs 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 inputThe answers to these decision The answers to these decision require different actionsrequire different actionsIntroduces the need for Introduces the need for control control statementsstatementsBooleanBooleanbooleanboolean is a primitive data type that is a primitive data type that holds one of two valuesholds one of two valuestruetruefalsefalseAny 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 valueAs apposed to a mathematical expression As apposed to a mathematical expression which returns a number valuewhich returns a number valueRelational OperatorsRelational OperatorsWhat 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 OperatorsBoolean expressions are just like Boolean expressions are just like mathematical expressionsmathematical expressionsBut 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 operatorsEx.Ex.testScore < 80testScore < 80testScore * 2 > 350testScore * 2 > 350if statementif statementConditional 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 statementsWhen the code reaches the if statement, When the code reaches the if statement, it evaluates the boolean expressionit evaluates the boolean expressionIf it is true, the <then block> is executedIf it is true, the <then block> is executedIf it is false, the <else block> is executedIf it is false, the <else block> is executedCalled a Called a branching statementbranching statement because it because it branches to a block of codebranches to a block of codeBankAccountBankAccount Revisted RevistedWithdraw 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 wantedIs 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. CompoundThe decision we have made only The decision we have made only resulted in one instructionresulted in one instructionMost 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
Download Decisions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Decisions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Decisions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?