Scala Lesson 7 – Conditionals | Dataplexa

Conditionals in Scala

In this lesson, you will learn about conditionals in Scala. Conditionals allow a program to make decisions based on conditions.

Using conditionals, your program can execute different blocks of code depending on whether a condition is true or false.


Why Conditionals Are Important

Real-world programs must make decisions. For example:

  • Checking if a user is logged in
  • Validating input values
  • Granting or denying access
  • Handling different scenarios in logic

Scala provides clean and expressive ways to handle decision-making.


The if Statement

The if statement executes a block of code when a condition is true.

val age = 20

if (age >= 18) {
  println("You are eligible to vote")
}

Here, the condition age >= 18 is checked. If it is true, the message is printed.


The if–else Statement

The if–else statement allows you to run one block of code when the condition is true and another block when it is false.

val marks = 45

if (marks >= 50) {
  println("Pass")
} else {
  println("Fail")
}

If the condition fails, the else block executes.


Multiple Conditions (else if)

You can check multiple conditions using else if.

val score = 82

if (score >= 90) {
  println("Grade A")
} else if (score >= 75) {
  println("Grade B")
} else if (score >= 60) {
  println("Grade C")
} else {
  println("Fail")
}

Conditions are checked from top to bottom. The first matching condition is executed.


if as an Expression (Very Important)

In Scala, if is not just a statement — it is an expression. This means it returns a value.

val number = 10

val result = if (number % 2 == 0) "Even" else "Odd"
println(result)

The if expression returns a value and assigns it to result.


Using if with Variables

Conditional expressions are often used to assign values dynamically.

val temperature = 30

val weather =
  if (temperature > 25) "Hot"
  else "Cool"

println(weather)

This makes Scala code concise and readable.


Nested if Statements

You can place one if statement inside another. This is called nesting.

val age = 22
val hasId = true

if (age >= 18) {
  if (hasId) {
    println("Access granted")
  } else {
    println("ID required")
  }
} else {
  println("Underage")
}

Nested conditionals are useful but should be used carefully to keep code readable.


Logical Operators in Conditions

You can combine multiple conditions using logical operators.

  • && — AND
  • || — OR
  • ! — NOT
val age = 25
val citizen = true

if (age >= 18 && citizen) {
  println("Eligible")
} else {
  println("Not eligible")
}

Both conditions must be true for the if block to execute.


Common Mistakes to Avoid

  • Forgetting curly braces for multiple statements
  • Using assignment (=) instead of comparison (==)
  • Writing very deep nested conditions

📝 Practice Exercises


Exercise 1

Check if a number is positive, negative, or zero.

Exercise 2

Assign a variable based on whether a number is even or odd.

Exercise 3

Check if a user can log in based on age and password status.


✅ Practice Answers


Answer 1

val num = -5

if (num > 0) {
  println("Positive")
} else if (num < 0) {
  println("Negative")
} else {
  println("Zero")
}

Answer 2

val n = 7
val result = if (n % 2 == 0) "Even" else "Odd"
println(result)

Answer 3

val age = 19
val passwordValid = true

if (age >= 18 && passwordValid) {
  println("Login successful")
} else {
  println("Login denied")
}

What’s Next?

In the next lesson, you will learn about Loops in Scala, which allow you to repeat actions efficiently.