A decision involves selecting from one of the two or more possible options. 

The if statement is used for selection or decision making.

Conditional Statements

1. If statement

Syntax:

if condition:

statement(s)

If the condition is true, then the indented statement(s) are executed. The indentation implies that its execution is dependent on the condition.

Example program:

Example1 - Teachoo.jpg

 

2. If….else statement

Syntax:

if condition: 

statement(s) 

else: 

statement(s)

If the condition is true, then the first indented statement(s) are executed. If the condition is false, then the second indented statement(s) are executed.

Example program:

Example - Teachoo.jpg

 

3. If…elif…else

Syntax:

if condition: 

statement(s) 

elif condition: 

statement(s) 

elif condition: 

statement(s) 

else: 

statement(s)

Number of elif is dependent on the number of conditions to be checked. If the first condition is false, then the next condition is checked, and so on. If one of the conditions is true, then the corresponding indented block executes, and the if statement terminates.

Example program:

Example2 - Teachoo.jpg

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo