Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Combining Multiple Conditions | Conditional Statements and Loops
Introduction to Scala
course content

Course Content

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

bookCombining 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): Returns true if both expressions are true, otherwise false.
java

Main

copy
12345678910
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): Returns true if at least one of the expressions is true, otherwise false.
java

Main

copy
1234567891011
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 is true, NOT returns false, and vice versa).
java

Main

copy
12345678910
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.

java

Main

copy
12345678910
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.

What will be printed?

What will be printed?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
some-alt