Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - Basic Operators and Operands

Basic Operators and Operands - Images: Japanese

Hey there, everyone!

Next up, let's use some basic operators in Swift to do arithmetic (addition, subtraction, and so on). While we're at it, we'll also cover operands.

We've already seen print() — and it turns out you can write arithmetic expressions right inside those parentheses. So let's give addition a try.

The symbol for addition is + — same as in math class. Let's write 1 + 1 inside the parentheses of print(). Like this:

print(1 + 1)

Run it, and here's what you get. Keep an eye on the debug area at the bottom of Xcode:

2

Looks like the calculation worked just fine. Writing '1 + 1' gives us back a result of '2'.

Pretty much what you'd expect, so maybe not the most thrilling thing ever — but hopefully it gives you a little taste of what programming feels like!

In programming, when we talk about the result of a calculation like addition, we use the phrase "returns a value."

In everyday math, you'd say "the answer to 1 + 1 is 2," right?

In programming, you'd phrase that as "the expression 1 + 1 returns 2."

It's a small difference in wording, but you'll hear it a lot in textbooks and on the job, so it's worth getting familiar with.

If it ever trips you up, just map it back to simple arithmetic and it'll click.

In Swift, the + symbol can also be used for string concatenation — not just addition. Just place + between two strings and you're good to go.

print("初音ミク" + "が好きです。") // The two strings get joined together, outputting '初音ミクが好きです。'.

String concatenation comes up constantly in programming, so make sure this one sticks.

Now let's run through all the symbols used for basic arithmetic in one go.

In Swift, addition uses +, subtraction uses -, multiplication uses *, division uses /, and the remainder (what's left over after division) uses %.

print(1 + 2) // Outputs the result of '1 + 2'
print(3 - 2) // Outputs the result of '3 - 2'
print(3 * 2) // Outputs the result of '3 × 2'
print(6 / 2) // Outputs the result of '6 ÷ 2'
print(6 % 5) // Outputs the remainder of '6 ÷ 5'

These are common across almost every programming language, so they're worth memorizing. You'll find the same symbols used in PC calculator apps too.

By the way, symbols and characters that have special effects like these are called operators.

And the values, numbers, or variables that an operation acts on are called operands. For example, in 1 + 2, the numbers 1 and 2 are both operands.

The terms operator and operand come up a lot, so keep them in mind.

One thing worth noting: in programming, we write '1 + 1' with spaces around the operator, not '1+1'. Like this:

print(1 + 2) // Write '1 + 2' with spaces around '+', not '1+2'

In other languages this is mostly just a style convention, but in Swift you really do need to put spaces on both sides of an operator.

The reason: Swift lets you define your own custom operators, and if you don't put spaces on both sides, Swift may not recognize the symbol as an operator correctly. This can lead to unexpected bugs, so watch out for it.

Swift provides operators like += and -= that perform addition or subtraction while also assigning the result back at the same time. You'll find the same thing in other languages too. Here are some examples:

var n = 1
n += 1 // Adds 1 to variable 'n' and assigns the result back to it
print(n) // Outputs 2

var n1 = 1
n1 -= 1 // Subtracts 1 from variable 'n1' and assigns the result back to it
print(n1) // Outputs 0

var n2 = 1
n2 *= 3 // Multiplies variable 'n2' by 3 and assigns the result back to it
print(n2) // Outputs 3

var n3 = 10
n3 /= 2 // Divides variable 'n3' by 2 and assigns the result back to it
print(n3) // Outputs 5

var n4 = 7
n4 %= 5 // Applies remainder division to variable 'n4' by 5 and assigns the result back to it
print(n4) // Outputs 2

Of these, '+=' and '-=' are the ones you'll use most often, so make sure you know them well.

Also, in Swift, += is used to add elements to an array, so keep that in the back of your mind too.

※ For more on arrays, check out this article.

In programming, you'll often hear the terms increment and decrement.

These refer to adding 1 to or subtracting 1 from a target operand, and they're built into almost every language.

Here's what they look like. The example below is JavaScript:

var n = 1;

++n; // Adds 1
--n; // Subtracts 1

If you've worked with other languages, this notation should look familiar.

Here's the thing though — in Swift, increment and decrement operators have been deprecated, so don't use them.

As of the current mainstream version of Swift (2016), they still work, but they're being dropped entirely starting around Swift 3.

So in Swift, to increment or decrement, just write it out plainly like this:

var n = 0

n = n + 1 // increment

print(n) // Outputs 1

n = n - 1 // decrement

print(n) // Outputs 0

Having used increment and decrement so much in other languages, it's easy to reach for them out of habit — so watch yourself there.

And that covers the basics of operators and operands. In the next article, we'll dig into booleans and comparison operators.

See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .