Arithmetic Operators

From Coder Merlin
Jump to navigation Jump to search

As you continue to learn about computer science and programming, you'll soon realize that math plays a fundamental role in both. From computer graphics in games to actual calculator programs, math plays an essential role in both. As such, it's important to have a solid understanding of the basic arithmetic operators you'll see most often when programming.

Note: You can find a much more detailed overview of operators as a whole in W1037 Expressions and Operators. However, this article will focus more on the basic arithmetic operators and offer more examples of each.

Addition[edit]

Addition works the way you'd expect in most programming languages. Simply use the + character between two numbers or variables.

print(5 + 5) // 10
print(12.25 + 4.3) // 16.55

Subtraction[edit]

To use subtraction while programming, simply use the - character between two numbers or variables.

print(15 - 10) // 5
print(4 - 105) // -101

Multiplication[edit]

To use multiplication while programming, simply use the * character between two numbers or variables.

print(10 * 3) // 30
print(100 * 0.25) // 25.0

About The Decimal Point[edit]

You may have noticed that in the second example, Swift added a decimal place even though it was zero. This has to do with how Swift handles numbers, which we'll talk about more in a later lesson.

Division[edit]

To use division while programming, simply use the / character between two numbers or variables.

print(100 / 4) // 25
print(10 / 0.25) // 40.0

"Rounding"[edit]

Notice that if you divide two integers that should have a decimal place, you just get the integer part:

print(100 / 3) // 33

Similar to multiplication, this has to do with how Swift deals with numeric data types. This will make more sense after learning about data types, but for now it's important to note that this isn't really rounding. All that's happening is everything after where the decimal point would be is just being lost:

print(100 / 51.0) // 1.9607843137254901
print(100 / 51) // 1

Modulo/Remainder[edit]

Technically, the modulo operator is different from just getting the remainder, but when working with two positive integers they function the same. In order to use the modulo operator, use the % character between two numbers or variables.

print(100 / 3) // 1
print(60 % 15) // 0

Simple Dollar Bill Example[edit]

A place where you might find modulo being useful is figuring out how many whole units can be used and what's left over. For example, if a customer would like to pay for a $97 order with five-dollar bills, you can use integer division to find how many dollar bills are needed:

print(97 / 5) // 19
print(97 % 5) // 2

As you can see, the customer can pay with 19 five-dollar bills and two one-dollar bills. Working backwards, we can see that 19 * 5 is 95, which is the closest we can get by using five-dollar bills. We can also see that 97 - 95 is two, which is what the modulo expression returned.


Designed with pride in Silicon Valley, CA, USA