Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CISC101 11/9/2018 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2018 CISC101 11/9/2018 CISC101 Reminders"— Presentation transcript:

1 Winter 2018 CISC101 11/9/2018 CISC101 Reminders Quiz 1 this week. More info in last Tuesday’s lecture. Assn 1 now due Monday, Feb. 5, next week. Exercise 2 is fair game now – work on console input and output techniques. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Conditionals (or “if” statements). Winter 2018
CISC101 - Prof. McLeod

3 So Far… Without conditionals programs are linear:
CISC101 So Far… Without conditionals programs are linear: Obtain data from user Calculations… Display results etc. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

4 Branching Algorithms If a program could test a condition, then it would have a choice as to its path of execution: if true if false etc. Winter 2018 CISC101 - Prof. McLeod

5 Conditionals or “Selection Statements”
Python has if, if-else and chained if (if-elif-else) statements. if statement syntax: if boolean_expression : statement1_when_true statement2_when_true statement3_when_true Example: if capacitance < 0 : print("Illegal capacitance") Winter 2018 CISC101 - Prof. McLeod

6 if-else Statement Syntax of if-else statement: Example:
if boolean_expression : statement(s)_when_true else : statement(s)_when_false Example: if stress > maxStress / 1.5 : result = "failure" result = "pass" Winter 2018 CISC101 - Prof. McLeod

7 if Statement, Cont. You will often have to nest if statements: etc.
Winter 2018 CISC101 - Prof. McLeod

8 Nested if Statements For example, if you have three numbers, a, b, & c and you want to print them to the screen in order: T F a < b T F T F b < c b < c T F T F a < c a < c abc cba acb cab bac bca See SortThree.py Winter 2018 CISC101 - Prof. McLeod

9 if-elif Statements In the code in SortThree.py, you might have noticed this construct: else : if a < c : This kind of thing occurs so often that it can be shortened to: elif a < c : Winter 2018 CISC101 - Prof. McLeod

10 if-elif Statements, Cont.
This leads to a common code structure often called a “chained if” construct: if condition1 : statement(s) elif condition2 : elif condition3 : else : You can have as many elifs as you want. The else part is optional. Winter 2018 CISC101 - Prof. McLeod

11 if-elif Statements, Cont.
There is nothing in this construct that you could not make with normal if-else statements. For some kinds of conditionals, the if-elif might be easier to put together. See Part B of assignment 1, for example. Winter 2018 CISC101 - Prof. McLeod

12 Nested if Statements - Demo
What to wear? Depends on time to destination and outdoor temperature: If time < 5 minutes, wear shorts & tshirt if time between 5 and 20 minutes: if temp >= 5 wear shorts & tshirt if temp between -10 and 5 wear pants & sweatshirt if temp less than -10 wear pants & jacket if time > 20 minutes: if temp >= 10 wear shorts & tshirt if temp between 0 and 10 wear shorts & sweatshirt if temp between – 10 and 0 wear pants & jacket if temp < -10 wear pants & parka Winter 2018 CISC101 - Prof. McLeod

13 Nested if Statements – Demo, Cont.
See: ClothesVersion0.py ClothesVersion1.py ClothesVersion2.py ClothesVersion3.py All work correctly and all do the same thing. Which one is “best” and why? Don’t forget that Python uses “short circuit” evaluation of logical operators – and & or. Winter 2018 CISC101 - Prof. McLeod

14 Aside – “Short Circuit” Evaluation
In the case of an and, if the LHS (“left hand side”) is already False then the RHS (“right hand side”) is not evaluated. Similarly for an or, if the LHS is already True, the RHS is not evaluated. Provides faster code execution! Winter 2018 CISC101 - Prof. McLeod

15 Building Conditionals
Nesting conditionals leads to the best structure for this example. If you have to nest conditionals, write the test with the fewest conditions for the outermost if statement. Make sure you have covered every possible combination of inputs. When testing, test every possible combination of inputs to make sure your logic is good. Writing nested conditionals can be tricky! Winter 2018 CISC101 - Prof. McLeod


Download ppt "Winter 2018 CISC101 11/9/2018 CISC101 Reminders"

Similar presentations


Ads by Google