Módulo 4 de 18 · Multimedia
If Else
The if...else statement
If statements are a perfect example on how to implement solutions that apply to our previous hotel problem. Instead of running a full program from top to bottom (from line 1 to the end of it) unquestioningly, we can define blocks of code that will run conditionally, and only when the condition evaluates to true.
As shown above, the way to do this is by typing an if keyword followed by parentheses and then by curly braces. Whatever lies inside the curly braces will be executed ONLY if the expression inside the parentheses evaluates to true! If not, then the whole block will be ignored.
Additionally you can define another block of code that will execute in case the first block condition evaluates to false. As an alternative in other words. This is done by defining an else case and then the block of code that will run in such cases. This else statement is connected to the if statement, that means ONLY ONE of these blocks will be executed at the end of the day.
The most important part to know, is that the expression inside the parentheses that follow an if statement must return either a 'truthy' or a 'falsy' value. But what are these values and how can we access them?