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 - How to Use Variables and Constants

How to Use Variables and Constants - Images: Japanese

Hey there, everyone!

In the previous article, we covered declaring variables and constants — nice work getting through all of that.

We went over how to define variables and constants, but we haven't really shown you how to actually use them yet.

So this time, let's write some simple code using variables and constants, and get a feel for just how powerful this concept really is.

First up, let's look at a pattern where storing a frequently used value in a variable or constant makes maintenance (editing) dramatically easier.

Here's a sample to start with.

print("I like Hatsune Miku.")
print("I love Hatsune Miku.")
print("I want to marry Hatsune Miku.")

This program is a lovely declaration of love for Hatsune Miku.

Now, life can take unexpected turns — what if you suddenly fell for IA instead?

With the code above, you'd have to update 3 separate places. Like this:

print("I like IA.") // You'd need to fix 3 places just like this.
print("I love IA.")
print("I want to marry IA.")

If this love poem program was 10,000 lines long, your brain things would get pretty rough.

Now let's use a variable or constant here. It would look something like this:

let lover: String = "Hatsune Miku"

print("I like " + lover + ".")
print("I love " + lover + ".")
print("I want to marry " + lover + ".")

If you're quick on the uptake, you've already figured it out.

Even if you switch from Hatsune Miku to IA, you just change the value of the constant lover and the update is reflected everywhere.

let lover: String = "IA" // Just fix this one spot and you're good.

print("I like " + lover + ".")
print("I love " + lover + ".")
print("I want to marry " + lover + ".")

Now it doesn't matter if you have 10,000 lines or 100,000 lines — no problem at all. That's what a proper update looks like.

This is one example of how variables and constants are used. Storing frequently used values in a variable or constant makes maintenance dramatically easier.

Along the same lines, here's another powerful pattern: storing the result of a computation in a variable or constant to avoid repeating it. Take a look at this sample.

print(1 + 1)
print(1 + 1)
print(1 + 1)
print(1 + 1)
print(1 + 1)

This just outputs the result of 1 + 1 five times.

Looking at this from a programming perspective — notice that you're making the computer perform the 1 + 1 calculation five separate times.

The result of 1 + 1 is always 2 — it never changes. In cases like this, you can reduce the load on the computer by storing the result in a variable or constant and using that instead.

let n = 1 + 1 // If the result doesn't change, store it in a variable or constant.

print(n) // Use the constant to print.
print(n)
print(n)
print(n)
print(n)

Keep this pattern in mind.

And here's one more sample worth knowing — variables can do something a bit tricky: you can use a variable in its own calculation and assign the result back to itself.

That might sound confusing, but take a look at the sample below. Can you tell what it outputs? This is something we've shown in other articles too, but it's a quirky way of computing that you don't see in math — it's unique to programming.

var n = 1

n = n + 1

print(n)

To cut right to it — the answer is 2.

Let's walk through it. The first line is straightforward: the variable n holds the value 1. We're using a variable (not a constant) since we're going to change the value.

var n = 1;

The tricky part is the next line.

n = n + 1;

A lot of people get tripped up here. Remember: in programming, execution flows from right to left — the right-hand side is evaluated first.

So focus on the right-hand side first. Before the right side is evaluated, the variable n holds the value 1. That means:

n + 1; // Variable 'n' is 1, so this expression is '1 + 1'.

And rewriting the result of that calculation gives us:

n = 2;

So the answer is 2.

Programming is a lot like math — in many ways it's essentially the same thing — but this is one of those tricky calculation patterns that's unique to programming and not something you'd see in a math class.

In Swift, you can expand a variable or constant inside a string using \(variable or constant). It looks like this:

let s = "Hatsune Miku"

print("I like \(s).") // Outputs 'I like Hatsune Miku.'

You wrap the variable or constant inside \( and ). This is used all the time, so make sure to remember it.

By the way, if the variable or constant you're expanding isn't a string, it gets converted automatically. Since Swift is generally strict about types, this is actually a bit of an unusual behavior — you might expect an error, but it doesn't throw one. Just something to be aware of.

let n = 1, b = true

print("\(n)") // Outputs '1'.
print("あいうえお\(b)") // Outputs 'あいうえおtrue'.

And that wraps up our look at how to use variables and constants. It was a quick tour through a few samples, but hopefully you got at least a sense of just how powerful and convenient this concept can be.

In the next article, we'll be covering arrays.

That's all for now — see you next time!

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 .