Welcome to the newest chapter of our journey through Scala: arithmetic and logical operations. These operations are the cornerstone of programming, facilitating calculations and decision-making. Let's dive right in!
Arithmetic operations in Scala include addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus (%
). The modulus operation, %
, calculates the remainder after dividing one number by another. For instance, 7 % 3
equals 1
, because 7 divided by 3 leaves a remainder of 1. We'll explore these operations through examples using variables a
and b
:
Scala1@main def run: Unit = 2 val a = 10 3 val b = 2 4 5 println(a + b) // prints: 12 6 println(a - b) // prints: 8 7 println(a * b) // prints: 20 8 println(a / b) // prints: 5 9 println(a % b) // prints 0, because the remainder of 10/2 is 0
Scala offers a means to streamline arithmetic operations combined with assignment through the use of compound assignment operators. These operators, which include +=
, -=
, *=
, /=
, and %=
, combine an arithmetic operation with an assignment to enhance code readability.
Scala1@main def run: Unit = 2 var score = 100 3 score += 10 // same as `score = score + 10`, score is now 110 4 score -= 20 // same as `score = score - 20`, score is now 90 5 score *= 2 // same as `score = score * 2`, score is now 180 6 score /= 3 // same as `score = score / 3`, score is now 60 7 score %= 7 // same as `score = score % 7`, score is now 4
Logical operations, specifically AND (&&
), OR (||
), and NOT (!
), are indispensable for making branching decisions in Scala. AND (&&
) returns true
only if both operands are true. OR (||
) returns true
if either operand is true. NOT (!
) inverts the boolean value. Here is an example of an in-game task:
Scala1@main def run: Unit = 2 val hasEnoughPoints = true 3 val hasEnoughCoins = false 4 5 println(hasEnoughPoints && hasEnoughCoins) // prints: false 6 println(hasEnoughPoints || hasEnoughCoins) // prints: true 7 println(!hasEnoughCoins) // prints: true
Arithmetic and logical operations can be combined. By using such combinations, let's illustrate the conditions for a game:
Scala1@main def run: Unit = 2 val points = 150 3 val coins = 5 4 val isPremiumMember = true 5 6 val canUnlockLevel = points > 100 && (coins > 10 || isPremiumMember) 7 println(canUnlockLevel) // prints: true
The expression points > 100 && (coins > 10 || isPremiumMember)
evaluates as follows: points > 100
returns true
because points
(150) is greater than 100, and inside the parentheses, coins > 10
returns false
because coins
(5) is not greater than 10, but isPremiumMember
is true
. With the ||
operator, the overall expression inside the parentheses is true
, resulting in true && true
, which makes canUnlockLevel
evaluate to true
.
Congratulations on becoming familiar with arithmetic and logical operations in Scala! You have mastered how to shape our programs and influence behavior based on specific conditions using these operations. This newly-acquired knowledge will be reinforced with some practice exercises. Are you ready? Let's get started!