Home Lesson-2.4

Lesson-2.3

Conditional Statements

if

Let us explore the idea of conditional statements by solving a simple problem:

Problem: Accept an integer as input from the user. If the number is greater than or equal to zero, print: non-negative.

Solution

if is a keyword in Python. The text adjacent to if is a boolean expression, usually called the if-condition or just the condition. Line-3 is the body of if. If the condition evaluates to True, then line-3 is executed. If it is False, then line-3 doesn't get executed. The following diagram captures the terms that have been introduced:

 

The control flow of the if-statement as a flow chart is given below:

 

Coming back to the code snippet:

Note that line-3 in the solution code is indented. In this case, the indentation corresponds to four spaces. It is very important to keep this consistent throughout the program. In all lessons, the first level of indentation will have four space . To understand how indentation works and why it is necessary, consider the following code blocks:

The output is:

Lines 3-5 in the code make up the if-block. Lines 4 and 5 which are indented make up the body of if. Whenever the if-condition evaluates to True, the interpreter enters the body of if and executes the lines sequentially. The indentation helps in separating the body of the if-block from the rest of the code.

Left: For the code on the left, the condition is True. So lines 4 and 5 are going to be executed. Once we exit the if-block, the interpreter will resume execution from line-6.

Right: For the code on the right, the condition is False. So, lines 4 and 5 are not going to be executed. The interpreter will skip the body of if and directly move to line-6.

if-else

Let us add one more level of complexity to the problem.

Problem

Accept an integer as input from the user. If the number is greater than or equal to zero, print: non-negative. If the number is less than zero, print negative.

Solution

else is a keyword in Python. When the if-condition evaluates to True, the statements inside the body of the if-block are evaluated. When the condition evaluates to False, the statements inside the body of the else-block are evaluated.

A visual representation of the control flow:

Points to remember:

The following code demonstrates the last two points:

 

if-elif-else

Time for another bump in the level of complexity:

Accept an integer as input from the user. If the number is greater than zero, print: positive. If the number is less than zero, print negative. If the number is equal to zero, print zero.

elif is a keyword in Python. It is a shorthand for else-if.

To understand how this works, let us consider three different inputs and the corresponding outputs.

InputOutput
x = 1positive
x = 0zero
x = -1negative

The entire if-elif-else block has three sub-blocks in it:

This is the process followed by the interpreter in executing the if-elif-else block:

A visual representation of the process is given below:

 

The general syntax:

Some features to note:

 

Nested conditional statements

Consider the following problem:

Accept three distinct integers as input from the user. If the numbers have been entered in ascending order, print in ascending order. If not, print not in ascending order.

An incomplete solution is given below:

The problem with the above solution is that it doesn't check if y < z. So, for an input like x, y, z = 1, 3, 2, it will print in ascending order, which is incorrect. The complete solution is given below:

Whenever a new if-block is introduced, its body should have exactly one level of indentation with respect to its if-condition. Since line-7 makes up the body of the if-block starting at line-6, it has one level of indentation with respect to line-6. However, line-6 is already at the first level of indentation with respect to line-5, so line-7 has two levels of indentation with respect to line-5. According to the convention we have chosen, two levels of indentation will correspond to eight spaces.

Having a conditional statement inside another conditional statement is called nesting. The if-block from lines 5-9 forms the outer block. The if-else block from lines 6-9 forms the inner block. The else in line-8 is paired with the if in line-6 as they are at the same level of indentation. For similar reasons, the else in line-10 is paired with the if in line-5.

 

Defining variables inside if

Consider the following snippet of code:

Run the code multiple times, varying the input each time. What do you observe?

Whenever the input is a multiple of 5, the code runs without any error. When the input is not divisible by 5, the code throws a NameError. This is because, we are trying to reference a variable that has not been defined. The variable output is created only if line-3 is executed during run-time. Its mere presence in the code is not enough.