Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
184_notes:if [2022/04/20 15:23] – created woodsna1 | 184_notes:if [2022/05/07 01:21] (current) – woodsna1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Coding: "If Statements" | ====== Coding: "If Statements" | ||
+ | ==== Lecture Video ==== | ||
+ | |||
+ | {{youtube> | ||
Sometimes, we only want the computer to execute code in specific cases. For instance, only divide if the denominator is nonzero. How can the computer decide if the denominator is nonzero? One way is by using an if statement: | Sometimes, we only want the computer to execute code in specific cases. For instance, only divide if the denominator is nonzero. How can the computer decide if the denominator is nonzero? One way is by using an if statement: | ||
< | < | ||
Line 7: | Line 10: | ||
quotient = numerator/ | quotient = numerator/ | ||
</ | </ | ||
- | Everything that follows the word " | + | Everything that follows the word " |
+ | |||
+ | ===== Else Clauses ===== | ||
+ | |||
+ | We often want the computer to do something else if the conditional evaluates to False. We can accomplish this with an " | ||
< | < | ||
if denominator != 0: | if denominator != 0: | ||
Line 14: | Line 21: | ||
quotient = 999999 | quotient = 999999 | ||
</ | </ | ||
- | In this code, the quotient will be set to some high number when the denominator is 0, instead of trying to calculate the true quotient. Code contained in else clauses only executes when the if statement' | + | In this code, the quotient will be set to some high number when the denominator is 0, instead of trying to calculate the true quotient. Code contained in else clauses only executes when the if statement' |
+ | |||
+ | ===== Nesting If Statements ===== | ||
+ | ==== Lecture Video ==== | ||
+ | {{youtube> | ||
Similarly to loops, we can use nested if statements: | Similarly to loops, we can use nested if statements: | ||
< | < |