Ask Question
1 March, 16:06

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

+5
Answers (1)
  1. 1 March, 19:29
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers