Ask Question
11 December, 19:55

Compare and contrast if-else and elif statements. Give examples.

+2
Answers (1)
  1. 11 December, 20:25
    0
    In coding, decisions are required only if certain conditions are fulfilled or met. If these conditions are met or not, the outcome should either be TRUE or FALSE before action of these statements is taken. An If else statement evaluates to FALSE when a Boolean expression is not met or is not true. Consider the snippet code below as an example

    If 10==10

    Print (10 is equal to 10)

    Else:

    Print (It is incorrect)

    So, the above means that the program will evaluate to true if 10==10, and will print "It is incorrect" if another value is provided that evaluates to false

    The elif statements, on the other hand, checks all conditions in your code and if one statement is TRUE, it will execute.

    if 5.5 = = int):

    print ("Value is Integer")

    elif 5.6 = = float):

    print ("Value is Float")

    elif 5.5 = = complex):

    print ("Value is Complex")

    else:

    print ("Unknown")

    Each condition is evaluated one after the other until a TRUE condition is found and evaluated. If all conditions happen to be FALSE, the else statement will be printed.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Compare and contrast if-else and elif statements. Give examples. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers