184_notes:if

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revisionBoth sides next revision
184_notes:if [2022/04/20 15:23] – created woodsna1184_notes:if [2022/04/20 15:24] woodsna1
Line 7: Line 7:
     quotient = numerator/denominator     quotient = numerator/denominator
 </code> </code>
-Everything that follows the word "if" is called the conditional. The code within an if statement will only execute if the conditional evaluates to True. In this case, we only compute the quotient for a nonzero denominator. You can read if statements out loud to understand what they're doing. In the above example, the if statement could be read as "if the denominator is not equal to 0, divide". We often want the computer to do something else if the conditional evaluates to False. We can accomplish this with an "else" clause:+Everything that follows the word "if" is called the conditional. The code within an if statement will only execute if the conditional evaluates to True. In this case, we only compute the quotient for a nonzero denominator. You can read if statements out loud to understand what they're doing. In the above example, the if statement could be read as "if the denominator is not equal to 0, divide". 
 + 
 +===== Else Clauses ===== 
 + 
 +We often want the computer to do something else if the conditional evaluates to False. We can accomplish this with an "else" clause:
 <code> <code>
 if denominator != 0: if denominator != 0:
Line 15: Line 19:
 </code> </code>
 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's conditional evaluates to False. 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's conditional evaluates to False.
 +
 +===== Nesting If Statements =====
  
 Similarly to loops, we can use nested if statements: Similarly to loops, we can use nested if statements:
  • 184_notes/if.txt
  • Last modified: 2022/05/07 01:21
  • by woodsna1