Learn
Conditionals
The πŸ™…β†ͺ️ Statement

So far we’ve only been able to check a single condition in our β†ͺ️/πŸ™… statements. But, we actually check more conditions by using the πŸ™…β†ͺ️ statement.

Like the β†ͺ️ that starts our conditionals, the πŸ™…β†ͺ️ accepts a condition and has a code block (πŸ‡β€¦πŸ‰) that executes if the condition is πŸ‘. One or more πŸ™…β†ͺ️ statements can be added after the β†ͺ️ statement. The πŸ™… statement will still be added at the very end as the default to run if the conditions for β†ͺ️ and πŸ™…β†ͺ️ are all πŸ‘Ž.

Let’s take a look at an example:

πŸ”€yellowπŸ”€ βž‘οΈπŸ–πŸ†• trafficLight β†ͺ️ trafficLight πŸ™Œ πŸ”€greenπŸ”€ πŸ‡ πŸ˜€ πŸ”€Go!!πŸ”€β—οΈ πŸ‰ πŸ™…β†ͺ️ trafficLight πŸ™Œ πŸ”€yellowπŸ”€ πŸ‡ πŸ˜€ πŸ”€Slow down...πŸ”€β—οΈ πŸ‰ πŸ™…β†ͺ️ trafficLight πŸ™Œ πŸ”€redπŸ”€ πŸ‡ πŸ˜€ πŸ”€Stop!πŸ”€β—οΈ πŸ‰ πŸ™… πŸ‡ πŸ˜€ πŸ”€This light's broken.πŸ”€β—οΈ πŸ‰ πŸ’­ Prints: Slow down...

In the example above, we included two πŸ™…β†ͺ️ statements. Each checked for different conditions. The first condition that evaluated to πŸ‘ executed its code block, so Slow down... was printed to the terminal. Once a code block has executed, the rest of the statements don’t run. If none of the conditions were πŸ‘, e.g. trafficLight had a value of πŸ”€brownπŸ”€, then πŸ™…β€˜s block runs.

Instructions

1.

Let’s make a program that tells us if what the phase of water is depending on the water temperature in Celcius.

Under the waterTemp variable, add a β†ͺ️ statement that checks if waterTemp is greater than or equal to 100. Then, in its code block (πŸ‡πŸ‰), use a πŸ˜€β—οΈ to print out the string: πŸ”€SteamπŸ”€.

2.

We also have to account for when waterTemp is in its liquid state.

After the β†ͺ️ statement, add a πŸ™…β†ͺ️ statement that checks if waterTemp is greater than 0. Then, in its code block (πŸ‡πŸ‰), use a πŸ˜€β—οΈ to print out the string: πŸ”€LiquidπŸ”€.

3.

What if our water is frozen?

After the πŸ™…β†ͺ️ statement, add another πŸ™…β†ͺ️ statement that checks if waterTemp is greater than -273. Then, in its code block (πŸ‡πŸ‰), use a πŸ˜€β—οΈ to print out the string: πŸ”€IceπŸ”€.

4.

What waterTemp isn’t a valid temperature?

Let’s add a πŸ™… statement just in case. Inside its code block (πŸ‡πŸ‰), use a πŸ˜€β—οΈ to print out the string: πŸ”€Temperature not validπŸ”€.

Folder Icon

Take this course for free

Already have an account?