If And Elif Blocks

If, elif, and else blocks check whether a certain condition is true or not and execute different code depending on that condition. The following sections describe how these statements work and provide working examples of each.

The If Statement
The if statement is used to test a condition. If the condition is true, the code wrapped by the if statement will be executed. Otherwise, if the condition is false, the code will be ignored. # Example If Statement Code x = 3 y = 4 if x == y: #Executed only if x is equal to y       x = x + y   #Not executed print x #prints 3

The Elif and Else Blocks
The elif call stands for else-if, meaning if the condition in the if statement is not satisfied it will check the condition in the elif statement next. Here are a couple of facts to remember about else and elif blocks.


 * 1) An elif always follows an if block.
 * 2) elif and else blocks are optional: you do not need to have any elif or else blocks after an if.
 * 3) The condition of the elif blocks are checked only if the condition in the if statement is false. If the if condition true, the conditions of the elif block are never checked and their code never executed.
 * 4) If any elif conditions are true, their code is run and the following elif and else blocks' conditions are never checked and their code never executed.
 * 5) An else block does not have a condition.
 * 6) If none of the conditions of the if and elif statements are true, the else block's code is executed.

# Incorrect Elif Block a = 2 b = 5 elif a < b :   #Causes an error because it must follow an 'if' block b = b + 5 # Correct Elif Block x = 3 y = 5 if x > y:       print "Hello World"    #Executed only if x is greater than y    elif x < y : print "What is up?" #Executed only if x is less than y   else : print "Good-Bye World" #Executed only if x is equal to y   #This code will out-put "What is up?"

Consecutive Elif Blocks
You can use any number of elif blocks (including 0) to help add extra conditions to decide which code should be ran. a = 65 letter = "NA" # No mark yet # Say that is your mark on a midterm and you want to know your letter grade if a < 50: letter = "F" elif a >= 50 and a < 60: letter = "D" elif a >= 60 and a < 70: letter = "C" elif a >= 70 and a < 80: letter = "B" elif a >= 80 and a < 90: letter = "A" else:                      #At this point it must be >= 90 letter = "A+" print letter #The resulting grade is a C

When Does an Else Block's Code Get Executed?
If all of the conditions for the if and elif are false, the code in the else block is executed. Else blocks are optional. This means that, if you do not have an else block and no conditions in the if and elif blocks are true, then the code just continues as usual.

# Let's use the code above but change the variable 'a' so that we hit #  the else block code a = 95 letter = "NA" if a < 50: letter = "F" elif a >= 50 and a < 60: letter = "D" elif a >= 60 and a < 70: letter = "C" elif a >= 70 and a < 80: letter = "B" elif a >= 80 and a < 90: letter = "A" else: letter = "A+" print letter #The resulting grade is an A+