Ask Question

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

+3
Answers (1)
  1. 9 June, 14:40
    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?
Find an answer to your question 👍 “Compare and contrast if-else and elif statements. Give examples. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers