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 πŸ”€Work timeπŸ”€.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?