Imagine a scenario where we’re thirsty and want to drink some lemonade. But it turns out all the lemonade was sold out. If we followed our original plan exactly, we wouldn’t get anything. But it’s more realistic to have some backup plan. In this case, we could get something else to quench our thirst.
Now let’s bring it back to programming. Recall that the code in the previous exercise our code would only execute if the condition was true (
👍). But there are many scenarios that we should have a backup plan ready for when the condition is false (
👎). That’s where the
🙅 statement comes in.
🙅 doesn’t accept a condition and is added after a
↪️ statement. The
🙅‘s block of code executes if the previous condition(s) are all
👎 (stay tuned, we’re going to cover multiple conditions soon in a later exercise!). Take a look at our code snippet:
👎 ➡️ 🖍🆕 isLemonadeInStock ↪️ isLemonadeInStock 🍇 😀 🔤One glass of lemonade, please!🔤❗️ 🍉 🙅 🍇 😀 🔤Water, please!🔤❗️ 🍉
Notice how our
↪️‘s condition is a variable
isLemonadeInStock. That’s right, we can use variables in our conditionals!
isLemonadeInStock has a value of
👎, so its code block doesn’t run. Rather our
🙅 runs and then
Water, please! is printed to the terminal. We can think of
🙅 as an else statement, its code block runs when all else is
After the existing
↪️ statement, add an
🙅 statement with a
🍇🍉 code block. Inside the code block, use
😀❗️ to print out the string