Session 6 Complex Conditionals CSC 104international, open membership, notfor-profit technology standards Programming Logic Pearson Custom consortium. Computer Science and Problem Solving CSC 104 Nassau Community College Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 1 From Last Class • Simple if statements with one condition and one possible action: if (grade = 100) then perfect score end if • Simple if/else statements one condition and two international, openwith membership, notpossible actions: for-profit technology standards if (grade >= consortium. 70) then pass else fail end if Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 2 Introduction to Logical Operators • Sometimes you want to evaluate two or more conditions in order to make a decision. international, open membership, • Conditions are combined using notlogical for-profit technology standards operators. consortium. • Logical operators (OR, AND, NOT) – explain complex conditions. Used in truth tables Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 3 Introduction to Logical Operators X NOT X True False False True True international, open membership, notY X OR Y X Y for-profit technology standards Trueconsortium. True True True True False True True False False False True True False True False False False False False False False X Prof. Vincent Costa Nassau Community College X AND Y True Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 4 Introduction to Logical Operators • Let’s say you want to determine whether or not it is the proper day/time for CSC104. How would you write that as a complex condition? international, open membership, notfor-profit technology standards if (day = consortium. “Tuesday”) AND (time = 1pm) then CSC 104 end if Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 5 Complex Conditions – If Statement • Structure: if (condition1) logicOperator (condition2) then statement(s) to be executed if the complex condition evaluates to TRUE else international, open membership, notfor-profit technology standardsif the complex statement(s) to be executed consortium. condition evaluates to FALSE end if • Logic Operators: AND, OR Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 6 Example 1 Complex if and if/else Results for each of the statement example different values if (average >= 70) AND values for average: (average < 80) then 60: open membership, grade isinternational, C 70: grade notis C for-profit technology standards end if 75: grade is C consortium. 80: Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 7 Example 2 Complex if and if/else Results for each of the statement example different values if (average >= 70) OR values for average: (average < 80) then 60: grade is C open membership, grade isinternational, C 70: grade notis C for-profit technology standards end if 75: grade is C consortium. 80: grade is C Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 8 Example 3 Complex if and if/else Results for each of the statement example different values if (age < 12) OR (age values for age: >=65) then 10: discounted ticket international, notdiscounted ticket open membership, 12: regular ticket for-profit technology standards else 25: regular ticket consortium. regular ticket 65: discounted ticket end if Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 9 Example 4 Complex if and if/else Results for each of the statement example different values if (age < 12) AND (age values for age: >=65) then 10: regular ticket international, notdiscounted ticket open membership, 12: regular ticket for-profit technology standards else 25: regular ticket consortium. regular ticket 65: regular ticket end if Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 10 More Logical Operators • You can use more than 1 logical operator in a complex condition – for example: if ((day = “Tuesday”) AND (time = 10am)) OR ((day =international, “Wednesday”) AND (time = 9:30am)) open membership, notthen for-profit technology standards CSC 104 consortium. end if • Order of precedence: use parenthesis, if no parenthesis ANDs are executed first then ORs. Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 11 Nested if/else • What if we have more than 2 possible actions or multiple values to be checked? • We can use additional conditions by placing one if statement within another if statement – this is called nesting if membership, statements. international, open notfor-profit technology standards • For example: Let’s say aconsortium. sporting event charges different prices based upon where a seat is located – seats in the lower balcony cost $100, seats in the middle section cost $60 and seats in the upper deck cost $25. How would you determine the seat price? Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 12 Nested if/else if (seatLocation = “lower balcony”) then price is $100 else if (seatLocation = “middle section”) then price is $60 international, open membership, notelse for-profit technology standards price isconsortium. $25 end if Notice: we don’t need to check if the seat is located in the upper deck because that is the only other option, known as the default Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 13 Review & Exercises • Review and do the exercises in Boolean Ops and Nested Conditional Exercises international, open membership, notfor-profit technology standards consortium. Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 14 Homework #4 • Use Blackboard or hand in on Wednesday or mail (vincent.costa@ncc.edu) • Please try to do it with Blackboard! international, open membership, notfor-profit technology standards consortium. Prof. Vincent Costa Nassau Community College Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights Reserved 15