GoConqr has a new look! Let us know what you think.

Module 4 of 18 · Media

If Else

Kostas Diakogiannis

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?

Instructions

You must submit an attempt for this quiz to progress. You can submit it after the last question.

Go to the chosen module

Feedback

What is it about?

What do you think of the new look?

How do you like it?