CHESAPEAKE CSC 103 - Chapter 4 Decision Structures & Boolean Logic

Unformatted text preview:

9/15/20081Chapter 4Decision Structures & Boolean LogicgBy Tony GaddisWith modifications by M. L. Malone (Sept 08)4-1Chapter Topics4.1 Introduction to Decision Structures4.2 Dual Alternative Decision Structures4.3 Comparing Strings44 Nested Decision Structures4.4 Nested Decision Structures4.5 The Case Structure4.6 Logical Operators4.7 Boolean Variables4-2What is a Decision Structure?• Allows a program to perform actions only under certain conditions•How different from SequenceHow different from Sequence Structures?4-3Types of Decision Structures• If (aka single alternative)• If then else (aka dual alternative)• Case (for multiple alternative decisions)4-4The if Statement• Action only occurs if the condition is trueIf condition ThenStatementStatementEnd If4-5Figure 4-1 A simple decision structureRelational Operators• Comparative!• Determines relationship between two values• Used within a Boolean expression (the condition)x > y x<y x >=y x<y x <=y x==y x!=y4-6Table 4-1 Relational operators9/15/20082Example Decision StructureIf hoursWorked > 40 thenCall CalcOvertimePay (hoursWorked, payRate, grossPay)Display “This employee worked overtime this week.”End ifEnd ifAs a flow chart?See Program 4.1 for algorithm using If then4-7Dual Alternative Decision Structure:• If then else – Executes one group of statements if its Boolean expression is true; a different group if its Boolean expression is falseFigure 48A dual alternative decision structure4-8Figure 4-8A dual alternative decision structureIf then elseIf condition ThenstatementstatementElseIf temperature < 40 ThenDisplay “A little cold”Display “Get a coat!”ElseElsestatementstatementEnd ifElseDisplay “Nice weather”Display “And sunny!”End if4-9If then elseIf hoursWorked > 40 thenCall calcOvertimePay (hoursWorked, payRate, grossPay)Display “This employee worked overtime this week.”ElseCallcalcRegularPay(hoursWorked,payRate,grossPay)Call calcRegularPay(hoursWorked, payRate, grossPay)Display “This employee did not work overtime this week.”End ifSee program 4‐2 using if‐then‐else4-10Comparing Strings• Allowed by most languages4-11Comparing Strings– OK to compare two stringsname1 == name 2– OK to compare a string and a literalMonth != “October”Si i ll ii–String comparisons are generally case sensitive– Relational operators generally allowed (why?)“Susan” <= “Susanna”“Tom” > “Thomas”“Annie < “annie” WHY?4-129/15/20083Comparing StringsWhat would be displayed if this were executed?Declare String str1= “Cambridge”Dl Si 2 “WMill ”Declare String str2= “WyeMills”If str1 > str2 thenDisplay str1, “ comes after “, str2ElseDisplay str2, “comes after “, str1End if4-13Nested Decision Structures• Why nest?– To test more than one condition– A decision structure is nested inside another decision structuredecision structure• How would you write this in pseudocode?4-14Figure 4-15A nested decision structureNested Decision Structures• Can make nested logic simpler to write• Other ways to write?• See flowchart on pg 139If score < 60 ThenDisplay “Grade is F.”pyElse If score < 70 ThenDisplay “Grade is D.”Else If score < 80 ThenDisplay “Grade is C.”Else If score < 90 ThenDisplay “Grade is B.”ElseDisplay “Grade is A.”End If4-15The Case Structure• Lets the value of a variable or an expression determine which path of execution the program will take• Can be used as an alternative to nested decisions• Under what circumstances good to use?4-16Figure 4-18 A case structureThe Case Structure in PseudocodeSelect someExpressionCase value_1:stmtstmt…Case value_2:stmtstmtA Boolean expression…Case value_3:stmtstmtstmt… etc…Default:stmtstmt…End Select4-17See flowchart for a sample case structure on pg 145.Logical Operators• aka Boolean operators• Used between conditions to create complex Boolean expressions•AND–Both conditions must betrueAND Both conditions must be true• OR –Either condition must be true• NOT –Reverses the truth of an expression4-189/15/20084Logical (aka Boolean) Operators• See pg 148‐150 for “Truth Tables” for AND, OR, and NOT• Short Circuit Evaluation of Boolean Expressions?Expressions?– Saves CPU time4-19Examples of Logical OperatorsAND exampleIf temperature < 20 AND minutes > 12 ThenDisplay “The temperature is in the danger zone.”End IfOR exampleIf temperature < 20 OR temperature > 100 ThenDisplay “The temperature is in the danger zone.”End IfNOT exampleIf NOT (temperature > 100) ThenDisplay “This is below the maximum temperature.”End If4-20Logical Operators for Range Checking• When checking for a number inside a rangeIf x >=20 AND x <=40 ThenDisplay “The value is in the acceptable range.”End If• When checking for a number outside a rangeIf x < 20 OR x >40 ThenDisplay “The value is outside the acceptable range.”End If4-21Boolean Variables• Of type Boolean• Can hold one or two values: true or falseDeclare Boolean isLunchTimeIf time >=12 thenSet isLunchTime = TrueElse Set isLunchTime = FalseEnd If4-22Using AND to simplify codeWithout ANDIf age > 18 thenIf registered==TRUE thenDisplay “Go vote!”With ANDIf (age > 18) AND (registered==true)Display “Go vote!”End ifpyEnd ifEnd if4-23Read/Study Chapter 4 • Test on chapter 1‐4 ONE WEEK FROM TODA Y• Study guide to be posted on course Web site– For now, the Review Questions at end of each chapter serve as a great way to practice/study forchapter serve as a great way to practice/study for the test– Part multiple choice, definitions, completion, and algorithm development using both pseudocodeand flow


View Full Document

CHESAPEAKE CSC 103 - Chapter 4 Decision Structures & Boolean Logic

Download Chapter 4 Decision Structures & Boolean Logic
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 Chapter 4 Decision Structures & Boolean Logic 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 Chapter 4 Decision Structures & Boolean Logic 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?