Conditional control statement type one in Python

 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

1. If statement:
  • It is used to check the condition and if true then it executes.
Syntax:

 if condition:
            True statement

Example program for if:

x=int(input("Enter any value: "))

if (x%2==0):

    print("Entered value is even")

Output:
if condition:
False case:
2. If – else statement:
  • It is used to check both conditions i.e. true and false.
  • It executes only one condition at a time.
Syntax:

if (condition):
          True statement
else:
          false statement

Example program for if-else:

x=int(input("Enter any value: "))

if (x%2==0):

    print("Entered value is even")

else:

    print("Entered value is odd")

Output:
if condition:
else condition:
3. Nested if....else statement:
  • Multiple if-else conditions are validating i.e. within condition check first after that execute.
Syntax:

if(condition 1):
          True statement1
          if(condition 2):
                   True statement2
          else:
                   False statement2
else:
          False statement1

Example program for nested if...else:

#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")


Output:
if-if condition:
if-if-else case:
else condition:
4. The if...else ladder:
  • It is similar to nested if but it checks continually.
Syntax:

if(condition):
          Statement1
elif(condition 2):
          Statement2
elif(condition 3):
          Statement3
else:
          Statement4

Example program for if...else ladder:

#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.")


Output:
if condition:
else if  condition:
else condition:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me