Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala | Decision Making

In Scala, as in most programming languages, you have constructs for decision-making that allow the execution of code blocks based on certain conditions. The most common constructs are if, if-else, and match (equivalent to the switch statement in some other languages).

1. if Statement

The simplest form of decision-making is the if statement.

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

2. if-else Statement

For branching decisions, you can use if-else.

val age = 15
if (age >= 18) {
  println("You are eligible to vote.")
} else {
  println("You are not eligible to vote.")
}

3. if-else-if Ladder

For multiple conditions, you can use the if-else-if ladder.

val score = 85
if (score >= 90) {
  println("Grade: A")
} else if (score >= 80) {
  println("Grade: B")
} else if (score >= 70) {
  println("Grade: C")
} else {
  println("Grade: F")
}

4. if Expression

In Scala, if can also be an expression that returns a value.

val age = 20
val eligibility = if (age >= 18) "eligible" else "not eligible"
println(s"You are $eligibility to vote.")

5. match Expression

Scala's match expression is a more powerful decision-making tool, similar to the switch statement in other languages but more expressive.

val day = "Monday"
val activity = day match {
  case "Sunday" => "Relax"
  case "Saturday" => "Go out"
  case _ => "Work"   // _ is a wildcard, similar to 'default' in a switch statement
}
println(s"On $day, I will $activity.")

With match, you can also perform pattern matching on various types, like tuples, lists, case classes, etc., making it a versatile tool for decision-making and de-structuring data.

In Conclusion

Decision-making constructs in Scala are both powerful and flexible. They allow for elegant and concise code while preserving the expressiveness required for complex logic.

  1. If-Else Statements in Scala:

    Use if-else for basic decision-making.

    val x = 10
    if (x > 5) {
      println("x is greater than 5")
    } else {
      println("x is not greater than 5")
    }
    
  2. Pattern Matching in Scala for Decision Making:

    Pattern matching offers a powerful way to make decisions based on data patterns.

    val dayOfWeek = "Monday"
    dayOfWeek match {
      case "Monday" => println("Start of the week")
      case "Friday" => println("End of the week")
      case _ => println("Some other day")
    }
    
  3. Using match Expression in Scala:

    The match expression is a concise form of pattern matching.

    val number = 3
    val result = number match {
      case 1 => "One"
      case 2 => "Two"
      case _ => "Other"
    }
    
  4. Nested Decision-Making in Scala:

    Nest if-else or match expressions for complex decisions.

    val x = 10
    val y = 5
    if (x > 5) {
      if (y > 3) {
        println("Nested condition satisfied")
      }
    }
    
  5. Guard Clauses in Scala Decision Making:

    Use guard clauses to add conditions to pattern matching.

    val number = 10
    number match {
      case x if x > 5 => println("Greater than 5")
      case x if x <= 5 => println("Less than or equal to 5")
    }
    
  6. Boolean Expressions and Decision Making in Scala:

    Boolean expressions can be used directly in decision-making.

    val x = 10
    val y = 5
    if (x > 5 && y > 3) {
      println("Both conditions satisfied")
    }
    
  7. Multiple Conditions with case in Scala Pattern Matching:

    Use | to match multiple patterns.

    val day = "Saturday"
    day match {
      case "Saturday" | "Sunday" => println("Weekend")
      case _ => println("Weekday")
    }