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 π
.
Instructions
After the existing βͺοΈ
statement, add an π
statement with a ππ
code block. Inside the code block, use πβοΈ
to print out the string π€Work timeπ€
.