Learn
Conditional Expressions
if/else Expression

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.

Instructions

1.

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

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

2.

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

3.

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

The total is [price] dollars.
Folder Icon

Take this course for free

Already have an account?