Branching statement in python:
- All the instructions are executed sequentially by default when no repetitions of some calculations are necessary.
- In some situation, we may have to change the execution order of statements based on condition or to repeat a set of the statement until certain conditions are met.
- These conditions are followed by indented space.
Tyes of branching statements:
1. If statement
2. If – else statement
3. Nested if....else statement
4. If ....else ladder
- It is used to check the condition and if true then it executes.
if condition: True statement
x=int(input("Enter
any value: "))
if
(x%2==0):
print("Entered value is even")
x=int(input("Enter
any value: "))
if
(x%2==0):
print("Entered value is even")
- It is used to check both conditions i.e. true and false.
- It executes only one condition at a time.
if (condition): True statementelse: false statement
x=int(input("Enter
any value: "))
if
(x%2==0):
print("Entered value is even")
else:
print("Entered value is odd")
x=int(input("Enter
any value: "))
if
(x%2==0):
print("Entered value is even")
else:
print("Entered value is odd")
- Multiple if-else conditions are validating i.e. within condition check first after that execute.
if(condition 1): True statement1 if(condition 2): True statement2 else: False statement2else: False statement1
#write a program based on 11th group selection
x=input("Enter pass or fail in 10th standard: ")
if x in 'pass,Pass':
mark=int(input("Entered total mark:"))
if(mark>=450):
print("You can choose all groups")
else:
print("You can also choose all group except first group")
else:
print("Entered correct spelling of pass")
#write a program based on 11th group selection
x=input("Enter pass or fail in 10th standard: ")
if x in 'pass,Pass':
mark=int(input("Entered total mark:"))
if(mark>=450):
print("You can choose all groups")
else:
print("You can also choose all group except first group")
else:
print("Entered correct spelling of pass")
- It is similar to nested if but it checks continually.
if(condition): Statement1elif(condition 2): Statement2elif(condition 3): Statement3else: Statement4
#Write a program to find biggest value among three numbers
a,b,c=input("Enter three value : ").split()
if a>b and a>c:
print(a+" is biggest value.")
elif b>c:
print(b+" is biggest vaue.")
else:
print(c+" is biggest value.")
#Write a program to find biggest value among three numbers
a,b,c=input("Enter three value : ").split()
if a>b and a>c:
print(a+" is biggest value.")
elif b>c:
print(b+" is biggest vaue.")
else:
print(c+" is biggest value.")
No comments:
Post a Comment