Unit 2: Branching

Conditional Statements

Conditional statements give the ability to check for a condition to execute statements if the condition is true. Conditions are written as a boolean expression after the if statement is called the condition. If it is true, then all the indented statements get executed. If not, then all the statements indented under the else clause get executed.

Flowchart of an if statement with an else clause

The syntax for an if statement looks like this:

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

if statement:

>>> if x % 2 == 0:
>>>     print(x, " is even.")
>>> else:
>>>     print(x, " is odd.")

Each of the statements inside the first block of statements is executed in order if the Boolean expression evaluates to True. The entire first block of statements is skipped if the Boolean expression evaluates to False, and instead, all the statements indented under the else clause are executed.

Omitting the else clause

Flowchart of an if statement with no else clause

Another form of the if statement is one in which the else clause is omitted entirely. In this case, when the condition evaluates to True, the statements are executed, otherwise, the flow of execution continues to the statement after the if.

>>> if x < 0:
>>>     print("The negative number is ",  x)

Chained Conditionals

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:

>>> if x < y:
>>>     STATEMENTS_A
>>> elif x > y:
>>>     STATEMENTS_B
>>> else:
>>>     STATEMENTS_C

Flowchart of this chained conditional

elif is an abbreviation of else if. Again, exactly one branch will be executed. There is no limit of the number of elif statements but only a single (and optional) final else statement is allowed and it must be the last branch in the statement:

>>> if choice == "a":
>>>     print(“Choice is a”)
>>> elif choice == "b":
>>>     print(“Choice is b”)
>>> elif choice == "c":
>>>     print(“Choice is c”)
>>> else:
>>>     print("Invalid choice.")

Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.

Nested conditionals

One conditional can also be nested within another. (It is the same theme of compossibility, again!) We could have written the previous example as follows: Flowchart of this nested conditional

>>> if x < y:
>>>     STATEMENTS_A
>>> else:
>>>     if x > y:
>>>         STATEMENTS_B
>>>     else:
>>>         STATEMENTS_C

The outer conditional contains two branches. The second branch contains another if statement, which has two branches of its own. Those two branches could contain conditional statements as well.

Although the indentation of the statements makes the structure apparent, nested conditionals very quickly become difficult to read. In general, it is a good idea to avoid them when we can.

Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional:

>>> if 0 < x:            # Assume x is an int here
>>>     if x < 10:
>>>         print("x is a positive single digit.")

The print function is called only if we make it past both the conditionals, so instead of the above which uses two if statements each with a simple condition, we could make a more complex condition using the and operator. Now we only need a single if statement:

>>> if 0 < x and x < 10:
>>>     print("x is a positive single digit.")