complex conditionals. n-way decisions how to deal with more than 2 alternates? nested else...

12
Complex Conditionals

Upload: griffin-cameron

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

else if

TRANSCRIPT

Page 1: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Complex Conditionals

Page 2: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

N-way decisions

• How to deal with more than 2 alternates?

• Nested else statements

Page 3: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

else if

Page 4: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

More complicated IF example

Let’s assign letter grades to students!

90 - 100 – A

80 - 89 – B

70 - 79 – C

60 - 69 – D

<60 – F

Page 5: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Decision Treegrade < 60

F grade < 70

D grade < 80

C grade < 90

B A

Page 6: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

nested else

Page 7: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Exercise

• Choose a number from 1 to 100

• If the number is greater than 50, STAND

• If you are seated and the number is greater than 25, RAISE YOUR RIGHT HAND

• If you are standing and the number is less than 75, RAISE YOUR RIGHT HAND

Page 8: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Exercise ResultsResults: 4 groups, 3 variables

• 0-25: seated, no hand raised

• 26-50: seated, right hand raised

• 51-75: standing, right hand raised

• 76-100: standing, no hand raised

Notice that raised right hand has completely different meanings based on context

Page 9: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

switch

Page 10: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

switch

• When checking the same variable for equal to different values

switch(variable) { case “val1”: stmt1; break; case “val2”: stmt2; break; default: stmt;}

Page 11: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Working with date

Page 12: Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements

Date and Time(to help understand snippets)

Full date is unfriendly format

• To get today’s date: var d = new Date();

• To get the time: var time = d.getHours();

• To get the day: var theDay = d.getDay();

w3schools

http://www.w3schools.com/js/js_date_methods.asp