Python Programming If Elif Else Statement Tutorial with Stone River eLearning

Following on the ‘if’, ‘else’ and the ‘if else’ statement, in this blog post we are going to talk about the ‘if…elif…else’ statements in Python. The inclusion of the ‘elif’ condition might be a little new for all the beginners in Python as they might feel that programming languages such as C, C++ do not really require or have the ‘elif’ statement, but actually ‘elif’ is just a shorter way of writing ‘elseif’. Having already discussed about the other flow control statements, in this blog post, we will pay more attention towards the ‘elif’ part and the whole combination of flow control statements.

How is ‘elif’ different from ‘else’?

The biggest difference between an else statement and a elif statement is that while the latter allows you to check multiple condition statements and execute a particular block of code as soon as the condition written inside holds true, ‘else’ statement can only come in operation when the condition inside the ‘if’ statement does not hold true.

Even though both ‘elif’ and th ‘else’ statements are optional in a Python code, a big difference is the number of ‘elif’ or the ‘else’ statements that allowed for a specified block of code. While we can at most have one else statement following an ’if’ condition, there is no such restriction on the ‘elif’ condition and we can have as many ‘elif’ as we want.

Many people often confuse ‘elif’ with consecutive ‘if’ statement execution, however; taking the performance and optimization of the program into consideration, an ‘elif’ statement is much better than the consecutive if statements.  It is because consecutive if require conditions to be checked for each one of them, however; the operation inside an elif statement is only executed when the ‘if’ and the ‘elif’ statements above it inside a block are not executed.

Eg:

x=2

y=3

z=5

if x > y:

print(x,’is greater than’,y)

elif x<y:

print(x,’is smaller than’,y)

elif y<z:

print(y,’is smaller than’,z)

else:

print(‘nothing was the case’)

Output:

2 is smaller than 3.

As you can see in the above example, only the part of the code that satisfies a condition runs while all other conditions after the successful part of the code are neglected. Once a condition holds true, the control breaks out of the block and all the conditions after that ‘elif’ part hold no significance in the final output on the console.

‘Or’, ‘And’ and ‘Break’ command:

The ‘or’ keyword is used to insert more than one condition in a flow control statement. The ‘and’ keyword is also used to insert more than one condition in a flow controls statement, however; both ‘or’ and ‘and’ have contrasting effects. While a ‘or’ keyword specifies that any one of the listed conditions must hold true, ‘and’ keyword means that all the conditions specified must be true to run the operation in the if statement.

The break statement is used to break the flow of commands in a code and jump the control to a different place, often to the place from where the block started executing. The break statement is used in programs involving big application where a variety of applications can be performed on each click.

Online Technology Courses Stone River eLearning
GET YOUR FREE PYTHON EBOOK!

Comments