Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.

Similar presentations


Presentation on theme: "1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction."— Presentation transcript:

1 1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Session 6 Complex Conditionals Pearson Custom Computer Science CSC 104 Nassau Community College CSC 104 Programming Logic and Problem Solving

2 2 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. 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 with one condition and two possible actions: if (grade >= 70) then pass else fail end if

3 3 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Introduction to Logical Operators Sometimes you want to evaluate two or more conditions in order to make a decision. Conditions are combined using logical operators. Logical operators (OR, AND, NOT) – explain complex conditions. Used in truth tables

4 4 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Introduction to Logical Operators XYX OR Y True FalseTrue FalseTrue False XYX AND Y True False TrueFalse XNOT X TrueFalse True

5 5 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. 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? if (day = “Tuesday”) AND (time = 1pm) then CSC 104 end if

6 6 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Complex Conditions – If Statement Structure: if (condition1) logicOperator (condition2) then statement(s) to be executed if the complex condition evaluates to TRUE else statement(s) to be executed if the complex condition evaluates to FALSE end if Logic Operators: AND, OR

7 7 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 1 Complex if and if/else statement example Results for each of the different values if (average >= 70) AND (average < 80) then grade is C end if values for average: 60: 70: grade is C 75: grade is C 80:

8 8 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 2 Complex if and if/else statement example Results for each of the different values if (average >= 70) OR (average < 80) then grade is C end if values for average: 60: grade is C 70: grade is C 75: grade is C 80: grade is C

9 9 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 3 Complex if and if/else statement example Results for each of the different values if (age =65) then discounted ticket else regular ticket end if values for age: 10: discounted ticket 12: regular ticket 25: regular ticket 65: discounted ticket

10 10 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 4 Complex if and if/else statement example Results for each of the different values if (age =65) then discounted ticket else regular ticket end if values for age: 10: regular ticket 12: regular ticket 25: regular ticket 65: regular ticket

11 11 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. 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 = “Wednesday”) AND (time = 9:30am)) then CSC 104 end if Order of precedence: use parenthesis, if no parenthesis ANDs are executed first then ORs.

12 12 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. 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 statements. For example: Let’s say a 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?

13 13 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Nested if/else if (seatLocation = “lower balcony”) then price is $100 else if (seatLocation = “middle section”) then price is $60 else price is $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

14 14 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Homework #4 Hand in on Tuesday or mail (vincent.costa@ncc.edu) to me over the weekendvincent.costa@ncc.edu


Download ppt "1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction."

Similar presentations


Ads by Google