Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arithmetic Operators | Variables and Data Types
Introduction to Scala
course content

Contenido del Curso

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

bookArithmetic Operators

Arithmetic operators are the building blocks of mathematical operations within Scala programs. These operators allow you to perform calculations on different values, enabling both simple and complex mathematical expressions. With this in mind, let's delve into this topic.

Addition

Let's first start with the simplest mathematical operation, addition. Adding two or more numbers in Scala is as simple as it can be: you should just use the + operator.

Let's take a look at an example:

java

Main

copy
123456789101112
object Main { def main(args: Array[String]): Unit = { val phonePrice = 1000 val watchPrice = 400 var totalCost = phonePrice + watchPrice // Adding two variables println(totalCost) totalCost = totalCost + 50 // Adding a number to a variable println(totalCost) totalCost = 35 + 10 // Adding two numbers println(totalCost) } }

As you can see, everything works just fine. We can add variables (mutable or immutable), numbers and variables, just numbers.

Subtraction

Subtraction in Scala works absolutely the same as addition, however, we use the - operator for performing subtraction. Let's take a look at an example:

java

Main

copy
12345678910111213
object Main { def main(args: Array[String]): Unit = { val phonePrice = 1000 val watchPrice = 400 val discount = 150 var totalCost = phonePrice + watchPrice - discount // Subtraction and addition println(totalCost) totalCost = totalCost - 50 // Subtracting a number from the variable println(totalCost) totalCost = 800 - 40 // Subtracting a number from another number println(totalCost) } }

Everything is rather straightforward here. Moreover, we can combine subtraction and addition within one expression.

Multiplication

To perform multiplication in Scala, you should use the * operator. Similarly to addition and subtraction, multiplication works with variables, numbers and combination of variables and numbers. Moreover, it can also be combined with any other mathematical operation.

java

Main

copy
12345678910111213141516
object Main { def main(args: Array[String]): Unit = { val bookPrice = 30 val numberOfBooks = 5 val discount = 10 val penPrice = 4 val numberOfPens = 2 // Multiplication, subtraction and addition var totalCost = bookPrice * numberOfBooks - discount + penPrice * numberOfPens println(totalCost) totalCost = totalCost * 10 // Multiplying a variable by a number println(totalCost) totalCost = 5 * 2 // Multiplying two numbers println(totalCost) } }

Division

To perform division in Scala, you should use the / operator. Everything we have discussed so far for other operations is applied to division as well, however, there are some specific behaviors to be aware of:

Integer Division

When both operands are integers (Int, Long, etc.), Scala performs integer division. In integer division, the result is an integer, and any fractional part is discarded (not rounded). For example, 10 / 3 will yield 3, not 3.33.

Floating-Point Division

When at least one of the operands is a floating-point number (Float or Double), Scala performs floating-point division. This division returns a floating-point result, preserving the fractional part. For example, 10.0 / 3 or 10 / 3.0 will yield something like 3.3333333333333334 (the rounding, however, may vary a bit).

Division by Zero

Division by zero is handled differently based on the types of the operands:

  • Integer Division by Zero: Results in an ArithmeticException.
  • Floating-Point Division by Zero: Results in Infinity or NaN (Not a Number). For example, 1.0 / 0 yields Infinity, and 0.0 / 0.0 yields NaN.
java

Main

copy
123456789
object Main { def main(args: Array[String]): Unit = { val booksCost = 400 val numberOfBooks = 5 // Dividing the cost of the books by their number to find out the price of a book val bookPrice = booksCost / numberOfBooks // Integer division (both operands are of type Int) println(bookPrice) } }

Remainder

In case you want to find the remainder of the division of one number by another, you can use the % operator. For example, 17 % 5 results in 2 since the closest number to 17 divisible by 5 is 15 and 17 -15 is equal to 2.

Let's take a look at another example. Suppose, I have a certain budget, say 1071, and I buy as many books as possible given my budget (the price of the book is equal to 80). My goal is to calculate the change that I will get in this case.

java

Main

copy
12345678910
object Main { def main(args: Array[String]): Unit = { val myBudget = 1071 val bookPrice = 80 /* Calculating the change provided that I buy maximum possible number of books */ val change = myBudget % bookPrice println(change) } }

The maximum number of books I can buy is the result of integer division of 1071 / 80. Let's call this number n, so the remainder is equal to 1071 - 80 * n which is exactly my change.

question-icon

Match the arithmetic operation with the corresponding operator.

Addition
Subtraction

Multiplication

Division

Remainder

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
some-alt