CONTROL FLOW
Decision making statements in programming languages decide the direction of the flow of program execution based on the given conditions.
Python supports if, Elif used to decide the flow of execution.
If statement:
It is the basic decision-making statement, in this the code will be executed based on the given condition.it is having a code body that only executes when the given condition is true.
Under one if there might be single or multiple statements executed based on given condition
Syntax:
1 2 |
1 if (condition): 2 statements |
Python uses indentation to identify a block so, the block under an if statement will be executed if the condition is true.
Flowchart of if in python
Example
1 2 3 4 5 6 7 8 |
1 #Example 1 2 age=20 3 if age>18: 4 print("Eligible For Voting") 5 #Example 2 6 m=10 7 if m>0: 8 print("Entered Value is Positive") |
OUTPUT
1 2 |
Eligible For Voting Entered Value is Positive |
if- else:
There exist two blocks of code one for if the condition is True other if the condition is False.
We can use the else statement with if statement to execute a block of code when the condition is False.
Syntax:
1 2 3 4 5 6 7 |
#if else syntax if(condition): Statements #above statements will be executed #when the condition is True else: Statements #above statements will be executed #when the condition is False |
Flowchart of if else in python
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
1 #Example 1 For if-else 2 m=10 3 if m>25: 4 print("Entered Value is Greater than 25 ") 5 else: 6 print("Entered Value is less than 25 ") 7 #Example 2 For if-else 8 m=45 9 if m>25: 10 print(m"Entered Value is Greater than 25 ") 11 else: 12 print("Entered Value is less than 25 ") |
OUTPUT
1 2 |
Entered Value is less than 25. Entered Value is Greater than 25. |
Nested if
An if statement inside another if statement is known as nested if.
Syntax
1 2 3 4 5 6 |
1 if(condition): 2 #if the condition is True 3 statements 4 if(condition): 5 #if the Condition is True 6 statements |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1 #check a number is divisible by 2 after by 5 2 n=50 3 if (n%2==0): 4 if (n%5==0): 5 print("Given Number is Divisible by 2 and 5") 6 else: 7 print("Not Divisible") 8 #with other Value 9 n=9 10 if (n%2==0): 11 if (n%5==0): 12 print("Given Number is Divisible by 2 and 5") 13 else: 14 print("Not Divisible 5") 15 else: 16 print("Not Divisible 2") |
Output:
1 2 |
Given Number is Divisible by 2 and 5 Not Divisible by 2 |
if -elif-else ladder
In Python, the if-elif-else condition statement has an elif block to chain multiple conditions one after another. This is useful when you need to check multiple conditions.
Syntax:
1 2 3 4 5 6 7 8 9 |
1 if (condition-1): 2 statement 1 3 elif (condition-2): 4 statement 2 5 elif (condition-3): 6 statement 3 7 ... 8 else: 9 statement n |
Example
1 2 3 4 5 6 7 8 9 |
1 n=2 2 if (n==1): 3 print("value of n is 1") 4 elif (n=2): 5 print("value of n is 2") 6 elif (n=3): 7 print("value of n is 3") 8 else: 9 print("value of n is 4") |
Output
1 |
value of n is 2. |
Author : Teja |
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform