Learn
Conditional Expressions
Order of Evaluation

We can combine expressions containing logical operators to create even more complex Boolean expressions. When combining logical operators, the order of evaluation is as follows:

  1. Boolean expressions contained within parentheses
  2. NOT - !
  3. AND - &&
  4. OR - ||

Take a look at the following snippet of code:

var isSnowing = true var isSunny = false var temp = 38 if (!isSunny && (temp > 40 || isSnowing)) { println("Wear a coat.") } else { println("Don't wear a coat.") }

Let’s discuss the order in which this expression is evaluated. Here is the Boolean expression we are evaluating:

!isSunny && (temp > 40 || isSnowing)

(temp > 40 || isSnowing) is evaluated first because the expression is contained within parentheses. This expression results in true, making our expression look like this:

!isSunny && true

!isSunny is evaluated next because of the NOT operator. This expression is true because it negates the value of isSunny:

true && true

Finally, we evaluate the expression using the logical AND operator (&&). Since both sides of the operator have a true value, the expression inside our if statement is true and we get the final output:

Wear a coat.

Instructions

1.

Use your understanding of logical operators and their order of evaluation to determine the value of the expression inside of the if expression.

If you believe the program will output "Hello", set the value of expressionValue to true.

If you believe the program will output "Goodbye", set the value of expressionValue to false.

Finally, use println() to output the value of expressionValue.

Note: Running the code will output the answer. Make sure to declare your variable and finalize your answer before you hit Run.

Folder Icon

Take this course for free

Already have an account?