Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Chained conditional is when you have more than two possibilities and we need more than two

branches. Using elif that is an abbreviation of “else if”. Again, exactly one branch will run.
There is no limit on the number of elif statements. If there is an else clause, it must be at the
end, but there doesn’t have to be one. Each condition is checked in order. If the first is false,
the next is checked, and so on. If one of them is true, the corresponding branch runs, and the
statement ends. Even if more than one condition is true, only the first true branch runs.
(Downey.A, 2015).

Example:

s= int(input("How many shoes do you have?\n"))

if s <= 2:

  print("It is enough?")

elif s >= 3 and s <= 5:

 print("Me too, more than this is unnecessary")

else:

 print("You have too much shoes, stop spending money with it.")

The output shows:


How many shoes do you have?

Me too, more than this is unnecessary

Nested conditionals is when you write an if inside another if. As the outer conditional contains
two branches, the first branch contains if statement, the second branch contains another if
statement (Downey.A, 2015).

Example:

h = int(input("What is your height in cm?\n"))

if h >= 180:

 h = input("Do you like basketball?\n ")

if h == "yes":

 print("Cool, me too!")

else:

 print("Let's play something.")


The output shows:
What is your height in cm?

170

Let's play something.

We can avoid nested conditionals by using the logical operators. simplifying nested
conditionals by combining both conditions using “and”, “or” and “not” (Downey.A, 2015).

Example not simple:

n = int(input("Please enter a ramdom number:"))

if 10 < n:    #first if

    if 100 > n:       #second if

        print("your number is greater than 10 and less than 100")

else:    

    print("I don't know your number.")

The output shows:


Please enter a ramdom number:50

your number is greater than 10 and less than 100

Example simple:

n = int(input("Please enter a ramdom number:"))

if 10 < n and 100 > n:    #using and to simplify

    print("your number is greater than 10 and less than 100")

else:    

    print("I don't know your number.")

The output shows:


Please enter a ramdom number:50

your number is greater than 10 and less than 100

You might also like