Often accompanying the if expression is another conditional called the else expression. The else expression executes a block of instructions when the condition in the if expression has a false value.

For example:

var isHerbivore = false var diet: String if (isHerbivore) { diet = "vegetables" } else { diet = "meat" } println("This animal can eat $diet.") // Prints: This animal can eat meat.

The else expression is not followed by a condition since it will only run code if the if expression’s condition is false.

This expression is always placed as the last conditional and cannot exist without an accompanying if expression.



Create an if expression that checks if the value of saleHappening is true.

Inside the if expression, set the value of price to 12.


Underneath the if expression, create an else expression that sets the value of price to 15.


Outside of the conditional expression, use println() and a string template to output the following statement:

The total is [price] dollars.

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?