Use of “else” with a conditional (and elif for Python)

--Originally published at Start in the world of the #TC101

It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false, if else.

flowchart_if_else.png

 

if BOOLEAN EXPRESSION:
    STATEMENTS_1        # executed if condition evaluates to True
else:
    STATEMENTS_2       # executed if condition evaluates to False

Example:


Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional

flowchart_chained_conditional.png

Example:

Source: http://www.openbookproject.net/books/bpp4awd/ch04.html