Course Content
Introduction to Scala
Introduction to Scala
Combining Multiple Conditions
Logical Operators
Understanding these operators is crucial for making decisions in your code based on multiple conditions, so let's take a look at them:
&&
(Logical AND): Returnstrue
if both expressions aretrue
, otherwisefalse
.
Main
object Main { def main(args: Array[String]): Unit = { var currentSpeed = 60 val speedLimit = 90 currentSpeed += 40 if ((currentSpeed > speedLimit) && (currentSpeed <= currentSpeed + 20)) { println("The speed limit is exceeded, however, no fines will be applied") } } }
The body of the if statement is executed if the combined condition is true
. Since we have the &&
(AND) operator both conditions should be true
to make the result true
(currentSpeed > speedLimit
should be true
AND
currentSpeed <= speedLimit + 20
should be true
). In our case, both of the conditions are indeed true
, so the result is also true
.
||
(Logical OR): Returnstrue
if at least one of the expressions istrue
, otherwisefalse
.
Main
object Main { def main(args: Array[String]): Unit = { var currentSpeed = 60 val speedLimit = 90 currentSpeed += 40 val isDrunk = true if ((currentSpeed > speedLimit + 20) || isDrunk) { println("The fines will be applied") } } }
The body of the if statement is executed if the combined condition is true
. Since we have the ||
(OR) operator at least one of the conditions should be true
to make the result true
(currentSpeed > speedLimit + 20
should be true
OR isDrunk
should be true
). In our case, the first condition is false
, however, isDrunk
is true
, so the overall result is true
.
!
(Logical NOT): Inverts the truth value of the expression (if the expression istrue
, NOT returnsfalse
, and vice versa).
Main
object Main { def main(args: Array[String]): Unit = { var currentSpeed = 30 val speedLimit = 90 currentSpeed += 40 if (!(currentSpeed > speedLimit)) { println("The speed is alright") } } }
Here, the body of the if statement is executed if the condition is true
. Since we have the !
(NOT) operator the condition in the parentheses ()
should be false
to make the result true
(currentSpeed > speedLimit
should be false
). In our case, the condition is indeed false
, so the overall result is true
.
Combining Logical Operators
In fact, you can form even more complex conditions by combining logical operators. Without further ado, let's take a look at the following example:
Suppose there is a certain major event in the city, and the guard at the entrance should determine whether a person can enter. In order to enter, a person should either have an invitation and be VIP (not a regular person) or have a pass. If the either of the conditions is met, the respective message should be printed.
Main
object Main { def main(args: Array[String]): Unit = { val hasInvitation = true val isRegularPerson = false val hasPass = false if ((hasInvitation && !isRegularPerson) || hasPass) { println("Welcome to the event.") } } }
As you can see, we have three Boolean
variables here which represent whether a person meets certain requirements.
In our case, the person doesn't have a pass, however, he/she has an invitation and is VIP (not a regular person), so the complex condition on the left is true
, and this person is welcomed to the event.
Thanks for your feedback!