198
edits
Changes
→PART 2 - IF/ELSE/ELIF
There are many ways to use if statements in more advanced configurations. This section will give a few examples and explain them.
:#For the following examples, try changing the numbers in the variables to get different results.:#Using if statements to do numeric comparisons, if variable 'a' is greater than variable 'b', this if statements condition will be True:<presource>
a = 10
b = 15
if a > b:
print('a is greater than b')
</presource> :#But we may want to know if 'a' is less than 'b'. The 'elif' statement allows us to string together multiple if statement. This new statement 'elif' means: IF the first condition is False, it will check the second condition under 'elif'. HOWEVER, if the first condition is True, it will run the code indented under the first condition and SKIP the 'elif' statement.<presource>
a = 10
b = 15
elif a < b:
print('b is greater than a')
</presource> :#In the event that we want to know if 'a' and 'b' are equal to each other, we could add another 'elif' using the '==' equal signs, but instead lets use 'else'. The 'else' statement will run the code indented under it only when all the 'if' and 'elif' statements above are False. <presource>
a = 10
b = 15
print('a is not greater than b')
print('a is not less than b')
</presource>
= INVESTIGATION 3: LOOPS =