In our last session, we saw how if
statements run code only when a condition is True
. Like checking age >= 18
before printing "You can vote!".
But what if the condition is False
?
Engagement Message
What should the program do then?
Often, we want to provide an alternative action when the if
condition isn't met. This is where the else
statement comes in handy! It specifies code to run only when the if
condition evaluates to False
.
Engagement Message
Think of it as the "otherwise" part of a decision. Make sense?
The else
statement always follows an if
block. It has a colon :
and its own indented block of code below it. It doesn't need its own condition because it catches everything the if
didn't.
