Conditional statements are fundamental in any programming language, allowing you to execute different code blocks based on certain conditions. In Scala, conditional statements include if, else if, and else. This section will cover the syntax and usage of these statements, along with practical examples and exercises.

Key Concepts

  1. If Statement: Executes a block of code if a specified condition is true.
  2. Else If Statement: Provides an additional condition if the initial if condition is false.
  3. Else Statement: Executes a block of code if none of the preceding conditions are true.
  4. Nested If Statements: Allows you to place an if statement inside another if statement.
  5. Ternary Operator: A shorthand for if-else statements.

If Statement

The if statement evaluates a condition and executes the associated block of code if the condition is true.

Syntax

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

Example

val number = 10

if (number > 0) {
  println("The number is positive.")
}

Explanation

  • number > 0 is the condition.
  • If the condition is true, println("The number is positive.") is executed.

Else If Statement

The else if statement allows you to check multiple conditions.

Syntax

if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition2 is true
}

Example

val number = 0

if (number > 0) {
  println("The number is positive.")
} else if (number == 0) {
  println("The number is zero.")
}

Explanation

  • The first condition number > 0 is false.
  • The second condition number == 0 is true, so println("The number is zero.") is executed.

Else Statement

The else statement executes a block of code if none of the preceding conditions are true.

Syntax

if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition2 is true
} else {
  // code to execute if none of the above conditions are true
}

Example

val number = -5

if (number > 0) {
  println("The number is positive.")
} else if (number == 0) {
  println("The number is zero.")
} else {
  println("The number is negative.")
}

Explanation

  • Both number > 0 and number == 0 are false.
  • The else block is executed, printing "The number is negative."

Nested If Statements

You can place an if statement inside another if statement to create more complex conditions.

Syntax

if (condition1) {
  if (condition2) {
    // code to execute if both condition1 and condition2 are true
  }
}

Example

val number = 15

if (number > 0) {
  if (number % 2 == 0) {
    println("The number is positive and even.")
  } else {
    println("The number is positive and odd.")
  }
}

Explanation

  • The outer if checks if number > 0.
  • The inner if checks if number % 2 == 0 to determine if the number is even or odd.

Ternary Operator

Scala does not have a traditional ternary operator like some other languages, but you can achieve similar functionality using if-else expressions.

Syntax

val result = if (condition) value1 else value2

Example

val number = 5
val result = if (number > 0) "positive" else "non-positive"
println(result)  // Output: positive

Explanation

  • The condition number > 0 is true, so result is assigned the value "positive".

Practical Exercises

Exercise 1: Positive, Negative, or Zero

Write a Scala program that checks if a number is positive, negative, or zero.

Solution

val number = -3

if (number > 0) {
  println("The number is positive.")
} else if (number == 0) {
  println("The number is zero.")
} else {
  println("The number is negative.")
}

Exercise 2: Even or Odd

Write a Scala program that checks if a number is even or odd.

Solution

val number = 4

if (number % 2 == 0) {
  println("The number is even.")
} else {
  println("The number is odd.")
}

Exercise 3: Nested If Statements

Write a Scala program that checks if a number is positive, negative, or zero, and if it is positive, also check if it is even or odd.

Solution

val number = 7

if (number > 0) {
  if (number % 2 == 0) {
    println("The number is positive and even.")
  } else {
    println("The number is positive and odd.")
  }
} else if (number == 0) {
  println("The number is zero.")
} else {
  println("The number is negative.")
}

Common Mistakes and Tips

  • Forgetting Braces: Always use braces {} for the code blocks, even if they contain a single statement, to avoid confusion and potential errors.
  • Condition Order: Ensure that conditions are ordered logically. For example, check for specific conditions before more general ones.
  • Readability: Use nested if statements judiciously to maintain code readability.

Conclusion

In this section, you learned about conditional statements in Scala, including if, else if, else, nested if statements, and the ternary operator equivalent. These constructs allow you to control the flow of your program based on different conditions. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics in the next module.

© Copyright 2024. All rights reserved