If Statements
The If Statement¶
The if statement has the following form, where the space is created by pressing tab on your computer:
if boolean:
execute code
What this means is that a boolean value passed after if is true, the code which is tabbed in it will be run. Otherwise it is skipped over. Of course that boolean can be created by testing a condition as well. Let’s begin first by looking at what happens when we use if followed by True and have code to print success.
#The way an if statement works is that anything tabbed in under the if statement executes if what follows if is true
if True:
print("Success")
When you have False, the code will not be executed.
#Notice this one won't print
if False:
print("Success")
The if statement only concerns that which is tabbed under it. There can also be multiple lines, so the following will print twice for example.
#This will print success twice
if True:
print("Success")
print("Success")
Things outside the tabbed area will still run normally whether or not the if statement was True. So the following will not print Success, but will print Not because that is outside the if statement.
#But anything outside an if statement still executes
if False:
print("Success")
print("Not")
If that were true though, both would get printed. Soon we will see how to handle the case where an if statement is not True.
#Both will execute
if True:
print("Success")
print("Not")
Combining what we have learned so far, you can also check and equality and use an if statement.
#Instead of true and false we can actually put a condition in
a = 5
if a == 5:
print("True")
An example where we want to divide a number by 5, and if that is equal to 2 we know that it must be a 10.
a = 10
if a/5 == 2:
print("It's 10")
Nesting If Statements¶
If statements are nested by using tab again. What this means is that those lines that are in the nested if statement must be tabbed twice! In the code below we show how we first check if a number is less than 10 and print out something, then check once again if it is less than 8 and if this is also true we print again! Notice, however, that you must tab in twice.
#You can also have multiple if statements within one and other
a = 5
if a < 10:
print("Less than 10")
if a<8:
print("Less than 8")
If only the outer if statement is true, then it will still be run but the inner if statement will not be run.
#Notice this time only the first indent's code is executed because only that condition is true
a = 9
if a<10:
print("Less than 10")
if a<8:
print("Less than 8")
Even if that inner statement is not run, if you write more code at the level of one tab in, it will still be run (as long as the outer if statement is still True.
#This will run code within the first if statement before and after checking the second if statement
if a<10:
print("Less than 10")
if a<8:
print("Less than 8")
print("This still gets executed!")
Also notice that if the outer if statement is false, nothing inside it gets run. So even if the inner if statement is true, it will not run. In this case we know a == 8 is true, but because it is nested within an if statement that is false is will never run!
#Nothing gets printed in here because the first statement is false
a = 8
if a==10:
print("Equals 10")
if a==8:
print("Equals 8")