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

06 Lecture Control Flow Statements Conditional Statements (If, If-Else, Elif) - 2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

06 Lecture Control Flow Statements Conditional Statements (If, If-Else, Elif) - 2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON Programming (CST310)

Lecture 6:
Flow Control Statements in Python
Conditional Statements (if, if-else, elif)

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
September 03, 2024
Flow Control Statements in Python
The flow control statements are divided into three categories
1. Conditional statements
2. Iterative statements.
3. Transfer statements.
Conditional statements in PYTHON
• In Python, condition statements act depending on whether a given
condition is true or false.
• You can execute different blocks of codes depending on the outcome
of a condition.
• Condition statements always evaluate to either True or False.
There are three types of conditional statements.
1.if statement
2.if-else
3.if-elif-else
If statement in Python
• In control statements, The if statement is the simplest form.
• It takes a condition and evaluates to either True or False.
• Only one condition is checked for True or False.
• If Condition comes to true, then only the block of code is executed.
• If the condition is False, then the block of code is skipped, and The
controller moves to the next line
Syntax of the if statement:
if(condition): if condition:
statement 1 statement 1
statement 2 or statement 2
statement n statement 3
Example of IF Statements in Python
number=11
if(number>10):
print(“value in variable number is greater than 10”)
If – else statement
• The if-else statement checks the condition and executes the if block
of code when the condition is True, and if the condition is False, it will
execute the else block of code.
Syntax of the if-else statement:
If(condition):
statement 1
statement 2
else:
statement 3
Example of IF-Else Statements in Python
password=input(“enter the password”)
if(password==‘NIT’):
print(“password is correct”)
print(“welcome”)
else:
print(“Incorrect Password”)
Chain multiple if statement in Python
• In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions one
after another.
• This is useful when you need to check multiple conditions.
Syntax of the if-elif-else statement:
if condition-1:
statement 1
elif condition-2:
stetement 2
elif condition-3:
stetement 3
...
else:
statement
Example of IF-Else Statements in Python
choice=input(“Enter your Choice”)
if(choice == 1):
print("Admin")
elif(choice == 2):
print("Editor")
elif(choice == 3):
print("Guest")
else:
print("Wrong entry")
Is it mandatory to have else block after the elif block?

If condition:
statement1
Elif condition2:
statement2
Practice Program:
WAP which test the following conditions:
• If the number is positive(>0), we print an appropriate message
• if number is 0, prints number is 0
• if number is less than 0, prints the message (no is less )
• else prints wrong input

You might also like