Learn
Learn Go: Conditionals
Logical Operators (And, Or)

In the previous exercises we checked one condition at a time. But what if we wanted to check multiple conditions at a time? To do so, we can use logical operators. There are three logical operators:

Operator Meaning:
&& And
|| Or
! Not

In this exercise, we’ll focus on the first two, && and ||. When we use the And (&&) operator, we are checking that both expressions are true:

if storeLights == "on" && doorsOpen { fmt.Println("You can enter the store!") }

When using the && operator, both conditions must evaluate to true for the entire condition to evaluate to true and execute. Otherwise, if either condition evaluate as false, the && condition will evaluate to false and the code inside the if block will not execute.

If we only care about either condition being true, we can use the Or (||) operator:

if day == "Saturday" || day == "Sunday" { fmt.Println("Enjoy the weekend!") } else { fmt.Println("Do some work.") }

When using the || operator, only one of the conditions must evaluate to true for the overall statement to evaluate to true. In the code example above, if either day == "Saturday" or day == "Sunday" is true the statement’s code block will execute. If the first operand in the || expression evaluates to true, the second operand won’t even be checked. Only if day == "Saturday" evaluates to false will day == "Sunday" be evaluated. The code in the else statement above will execute only if both comparisons evaluate to false.

Let’s implement these operators to add an additional layer of logic!

Instructions

1.

In the first conditional provided, in addition to checking rightTime, use the && operator to check if rightPlace is also true.

2.

In the second conditional provided, use the || operator to check for enoughBags.

Folder Icon

Take this course for free

Already have an account?