How to Add a Second “if” Statement | ELIF
Originally published: 27/11/2018 12:49
Publication number: ELQ-88178-1
View all versions & Certificate
certified

How to Add a Second “if” Statement | ELIF

Learn an elegant way of adding a second “if” statement to one of our expressions.

  • Step n°1 |

    Else if, for Brief – ELIF

    In this lesson, we’ll learn an elegant way of adding a second “if” statement to one of our expressions. This is done with the help of the elif keyword, as shown in this example.


    If y is not greater than 5, the computer will think: “else if y is less than 5”, written “elif y is less than 5”, then I will print out “Less”. And the else-statement follows as a tail with the respective block that says “return “Equal”.


    Let’s confirm we wrote the code correctly.


    We can print out the “compare to five” function with a value of y equal to 10 in the following way… then we’ll expect to see a statement that says “Greater” because 10 is greater than 5. Correct?
    How to Add a Second “if” Statement | ELIF image
  • Step n°2 |

    Ok. Perfect.

    What if we carry out this operation for the number 2? The machine tells us that 2 is less than 5. And that’s what we expected.


    To obtain the third outcome, we must compare the number 5 with a number that is not greater or smaller than 5. This will happen only if the argument of the function is 5, right? Shall we try this one?
    Great! We obtained “Equal”, as expected.
    How to Add a Second “if” Statement | ELIF image
add_shopping_cartContinue reading for free (70% left)


0.0 / 5 (0 votes)

please wait...