In this section, we will cover the fundamental operations and expressions in Scala. Understanding these basics is crucial as they form the foundation for more complex programming concepts. We will explore arithmetic operations, relational and logical operations, and how to use expressions effectively in Scala.

  1. Arithmetic Operations

Arithmetic operations in Scala are similar to those in other programming languages. Here are the basic arithmetic operators:

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 6 / 3 2
% Modulus 5 % 3 2

Example:

object ArithmeticOperations {
  def main(args: Array[String]): Unit = {
    val a = 10
    val b = 5

    println(s"Addition: $a + $b = ${a + b}")
    println(s"Subtraction: $a - $b = ${a - b}")
    println(s"Multiplication: $a * $b = ${a * b}")
    println(s"Division: $a / $b = ${a / b}")
    println(s"Modulus: $a % $b = ${a % b}")
  }
}

Explanation:

  • val a = 10 and val b = 5 declare two immutable variables.
  • The println statements print the results of the arithmetic operations.

  1. Relational Operations

Relational operators are used to compare two values. The result of a relational operation is a Boolean value (true or false).

Operator Description Example Result
== Equal to 5 == 3 false
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 3 true
<= Less than or equal to 5 <= 3 false

Example:

object RelationalOperations {
  def main(args: Array[String]): Unit = {
    val x = 7
    val y = 10

    println(s"$x == $y: ${x == y}")
    println(s"$x != $y: ${x != y}")
    println(s"$x > $y: ${x > y}")
    println(s"$x < $y: ${x < y}")
    println(s"$x >= $y: ${x >= y}")
    println(s"$x <= $y: ${x <= y}")
  }
}

Explanation:

  • val x = 7 and val y = 10 declare two immutable variables.
  • The println statements print the results of the relational operations.

  1. Logical Operations

Logical operators are used to combine multiple Boolean expressions.

Operator Description Example Result
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false

Example:

object LogicalOperations {
  def main(args: Array[String]): Unit = {
    val a = true
    val b = false

    println(s"$a && $b: ${a && b}")
    println(s"$a || $b: ${a || b}")
    println(s"!$a: ${!a}")
  }
}

Explanation:

  • val a = true and val b = false declare two immutable Boolean variables.
  • The println statements print the results of the logical operations.

  1. Expressions

An expression is a combination of variables, operations, and values that yields a result. In Scala, almost everything is an expression, including if statements.

Example:

object Expressions {
  def main(args: Array[String]): Unit = {
    val x = 10
    val y = 20

    // Arithmetic expression
    val sum = x + y
    println(s"Sum: $sum")

    // Relational expression
    val isEqual = x == y
    println(s"Is Equal: $isEqual")

    // Logical expression
    val isBothPositive = (x > 0) && (y > 0)
    println(s"Are both positive: $isBothPositive")

    // If expression
    val max = if (x > y) x else y
    println(s"Max: $max")
  }
}

Explanation:

  • val sum = x + y is an arithmetic expression that calculates the sum of x and y.
  • val isEqual = x == y is a relational expression that checks if x is equal to y.
  • val isBothPositive = (x > 0) && (y > 0) is a logical expression that checks if both x and y are positive.
  • val max = if (x > y) x else y is an if expression that determines the maximum of x and y.

Practical Exercises

Exercise 1:

Write a Scala program that takes two integers as input and performs the following operations:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Modulus

Solution:

object BasicOperationsExercise {
  def main(args: Array[String]): Unit = {
    val a = 15
    val b = 4

    println(s"Addition: $a + $b = ${a + b}")
    println(s"Subtraction: $a - $b = ${a - b}")
    println(s"Multiplication: $a * $b = ${a * b}")
    println(s"Division: $a / $b = ${a / b}")
    println(s"Modulus: $a % $b = ${a % b}")
  }
}

Exercise 2:

Write a Scala program that takes a Boolean value and performs the following logical operations:

  1. Logical AND with true
  2. Logical OR with false
  3. Logical NOT

Solution:

object LogicalOperationsExercise {
  def main(args: Array[String]): Unit = {
    val a = true

    println(s"$a && true: ${a && true}")
    println(s"$a || false: ${a || false}")
    println(s"!$a: ${!a}")
  }
}

Conclusion

In this section, we covered the basic operations and expressions in Scala, including arithmetic, relational, and logical operations. We also explored how to use expressions effectively. These fundamentals are essential for writing more complex Scala programs. In the next module, we will delve into control structures and functions, which will build upon the concepts learned here.

© Copyright 2024. All rights reserved