1,234
edits
Changes
→PART 2 - Using IF/ELIF/ELSE Statements
:There are many ways to use IF statements in more advanced configurations. This section will provide a few more examples and provide an explanation of how they work.
a = 10
b = 15
print('a is greater than b')
</source>What happened? Let's now use an IF/ELSE statement.<br><br>
a = 10
b = 15
print('b is greater than a')
</source>What happened?<br><br>This is neat, but what if 'a' is equal to 'b'? In order to make this work, we would need to perform another test! 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. Finally, we include the ELSE statement - in this case, if 'a' is equal to 'b' (i.e. fails first test since 'a' is not greater than 'b' and fails second test since 'a' is not less than 'b', so they must be equal to check other).<br><br>
a = 10
b = 10
print('Therefore, a is equal to b')
</source>
<br><br>