Python:else-if Ladder

It is a conditional statement used for selection between multiple set of statements based on multiple test conditions. The various test conditions are provided inside each if statement. Whenever the test condition is evaluated as True, the statements inside the corresponding if block are executed and the control comes out of the else-if ladder. If none of the test conditions are evaluated as True, the statements inside the else block are executed. As we have multiple set of statements to select based on the test conditions, it is also called as multi way selection statement.

In else-if ladder the conditions are evaluated from the top of the ladder downwards. As soon as a true condition is found, the statement associated with it is executed skipping the rest of the ladder.

Below is the syntax of else-if ladder statement:

Python else-if statement

Example

	a=0
	if(a>0):
	    print("positive integer")
	elif(a<0):
	    print("negative integer")
	else:
	    print("it’s zero") 

Output

It’s zero

In this above example, first evaluate the condition in if but a=0 so condition in if statement will result as false. Then it will go to next else if statement to evaluate the condition but the condition evaluation value will be false then it will go to else statement and execute all statement inside of it.