Real programs don't just do one thing; they adapt to the situation. For example, your web browser doesn't just show you the page by slowly and automatically scrolling through it. It listens to your input. When you scroll up, it moves the page up. When you scroll down, it moves the page down. So it has the ability to make decisions based on your input. We'd like to start doing that in our programs as well. Conditional statements are essential for it, as we'll see.
if
statementsAn if
statement is one of the most commonly used tools in a computer
programmer's toolbox. It's important to understand how if
statements work.
Let's start with an example:
As you can see, the second print
statement did not get executed. You may have
gathered by now that an if
statement is able to decide whether certain
statements get executed based on the value of a boolean expression. In the first
case, the n == 5
boolean expression evaluates to True
, causing the print
statement to execute. But the second boolean expression evaluates to False
and
that's why the second print
statement never executes.
The diagram below shows the basic structure of an if statement.
As you can see, the statements are only executed if the if
statement allows
them to.
It's important to note that the "contents" of an if
statement are have spaces
before them. The if
statement can have multiple python statements inside of it.
They are together (if
statement + contents) called the if
block.
Take a look at this example:
if-else
statementWhat if we wanted to execute some statements if a condition was True
and a
separate set of statements if it was False
? Well, we could always do something
like:
Unfortunately, as your boolean expression grows in complexity, coming up with
the negative version of it can be tedious and error-prone (and not to mention,
unreadable). Fortunately, most programming languages (including Python) provide
an easier way of expressing this intent using an if-else
statement:
The two examples above are identical, but one should prefer to use the second
form (the one involving else
). Try changing the value of n
in both of
the examples above to trigger the 2 print
statements.
The diagram below shows the basic structure of an if-else
statement:
elif
)What if you wanted to use multiple boolean expressions to decide what to run?
Specifically, what if you wanted to evaluate a second boolean expression if a
first boolean expression evaluates to False
?
You could get away with something like:
Note that we "nested" a second if-else
statement inside the else
clause of
the first. This is called "Nested conditionals". See the diagram below to help
you visualize how nested conditionals work:
While nested conditionals are useful, in this case we can simplify the logic by
using an elif
(short for "else-if") clause:
That's much easier to read and in programming, readability counts for quite a
lot. The great thing about elif
is that you can have as many of them as you
want:
The example above combines multiple concepts that we've learnt so far into a single program. Mess around with it and have fun! Try adding a few colors of your own to the choices.
© 2024 Abhin Chhabra | Sitemap