Conditional Statements and Loops

Conditional Statements

Scala supports several types of conditional statements that allow you to control the flow of your program. Here are the key conditional constructs with examples:

if Statement

The basic if statement works similarly to other languages. It evaluates a condition and executes the corresponding block if the condition is true.

if (condition) {
// code to execute if the condition is true
}

Example :

val x = 10
if (x > 5) {
println("x is greater than 5")
}

o/p:
x is greater than 5

if-else Statement

The if-else statement allows you to specify an alternative block of code to execute if the condition is false.

if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}

Example :

val x = 3
if (x > 5) {
println("x is greater than 5")
} else {
println("x is less than or equal to 5")
}

o/p:
x is less than or equal to 5

if-else if-else Statement

You can chain multiple conditions together using else if.

if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if both conditions are false
}

Example :

val x = 7
if (x < 5) {
println("x is less than 5")
} else if (x == 7) {
println("x is equal to 7")
} else {
println("x is greater than 7")
}

o/p:
x is equal to 7

Ternary-Like Expression (if as an expression)

In Scala, if is an expression, meaning it can return a value. This makes it similar to a ternary operator in languages like Java or C++.

val result = if (condition) value_if_true else value_if_false

Example :

val x = 10
val result = if (x > 5) "greater" else "less or equal"
println(result)

o/p:
greater

match Statement (Pattern Matching)

Scala has a more powerful conditional structure called match, which is similar to a switch statement in other languages. It’s used for pattern matching and can handle complex conditional logic.

value match {
case pattern1 => // code to execute if value matches pattern1
case pattern2 => // code to execute if value matches pattern2
case _ => // code to execute if none of the patterns match (default case)
}

Example :

val day = "Monday"
day match {
case "Monday" => println("Start of the week")
case "Friday" => println("End of the workweek")
case _ => println("Midweek day")
}

o/p:
Start of the week

match Expression

Just like if, the match statement can return a value. This makes it an expression.

val x = 2
val result = x match {
case 1 => "one"
case 2 => "two"
case _ => "other"
}
println(result)

o/p:
two

Summary:

  • if: Single condition.
  • if-else: Two-way decision.
  • if-else if-else: Multiple conditions.
  • match: Pattern matching for multiple cases, similar to switch but more powerful.

These constructs give Scala powerful conditional logic handling capabilities.


Loops

Scala provides several types of loop constructs that can be used for repetitive tasks. These include while, do-while, and for loops, along with some functional looping mechanisms such as foreach. Here’s an overview of each with examples: