0% found this document useful (0 votes)
19 views12 pages

If Statements

The document discusses different types of if statements in Python including: - Basic if statement that executes code if a condition is true - if/else statement that executes one block of code if true and another if false - Nested if statements that contain if statements within other if/else blocks - if/elif statement that allows evaluating multiple conditions in sequence - if/elif/else statement that provides an else block to handle all other cases
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

If Statements

The document discusses different types of if statements in Python including: - Basic if statement that executes code if a condition is true - if/else statement that executes one block of code if true and another if false - Nested if statements that contain if statements within other if/else blocks - if/elif statement that allows evaluating multiple conditions in sequence - if/elif/else statement that provides an else block to handle all other cases
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

If Statement

It is used to execute an instruction or block of instructions only if a condition is fulfilled.


Syntax: -
First the condition is tested, If the condition is True
if (condition):
statement
then the statements given after
Rest of the Code
colon (:) are executed
if (condition):
statement1
statement2 Block of statement/ Group of statements/ Suite
Rest of the Code

If there is single statement it can be written in one line.


Ex:- if (condition): Statement
If

condition

True
False
Block of Statement

Exit

Rest of the Code


Nested If Statement
Syntax :
if (condition):
block of statements
if(condition):
block of statements
if(condition):
block of statements
if(condition):
block of statements
Rest of the code
If Statement with Logical Operator
if ( (condition1) and (condition2) ):
Statement
Rest of the Code

if ( (condition1) and (condition2) ):


Block of Statements
Rest of the Code

if ( (condition1) or (condition2) ):
Statement
Rest of the Code
Indentation
Indentation refers to spaces that are used in the beginning of a statement. By
default python puts 4 spaces but it can be changed by programmers.
if (condition):
____Statement
____if(condition):
________statement 1
________statement 2
____if(condition):
________Statement
if(condition):
____Statement 1
____Statement 2
Rest of the code
If Else Statement
if… else statement is used when a different sequence of instructions is to be executed
depending on the logical value(True/False) of the condition evaluated.
Syntax: - if(condition):
Statement 1
if(condition): Statement 2
Statement 1 else:
else: Statement 3
Statement 2 Statement 4
Rest of the Code Rest of the Code
If

Condition ? False

True

Statement 1 Statement 2

Exit

Rest of the Code


Nested If Else Statement
In nested if…. else statement, an entire if… else construct is
written within either the body of the if statement or the body of
an else statement.
Nested If Else Statement
if(condition_1): if(condition_1):
if(condition_2): if(condition_2):
Statement 1 Statement 1
else:
else:
Statement 2
Statement 2 else:
else: if(condition_3):
Statement 3 Statement 3
Rest of the Code else:
Statement 4
Rest of the Code
if elif Statement
To show a multi-way decision based on several conditions, we use if elif statement.
if (condition_1):
Statement 1
elif (condition_2):
Statement 2
elif (condition_3):
Statement 3
elif (condition_n):
Statement n
Rest of the Code
if elif else Statement
To show a multi-way decision based on several conditions, we use if elif else
statement.
if (condition_1):
Statement 1
elif (condition_2):
Statement 2
elif (condition_3):
Statement 3
elif (condition_n):
Statement n
else:
Statements x
Rest of the Code
Condition_1 ? False

Condition_2 ? False
True

Condition_3 ? False
True
Statement 1

Condition_n ? False
True
Statement 2
Statement x

True
Statement 3

Statement n

Exit

Rest of the Code

You might also like