Operations on Variables

Created By: Geoffrey Challen
/ Updated: 2022-08-07

Next we'll continue introducing the basic building blocks of our computer programs. We'll show how you can manipulate the data stored by your variables through simple mathematical operations.

Reminder: Variables

In the last lesson we introduced variables, which allow us to store changing values as our program runs. We looked at how to declare and initialize those variables. The declaration tells Kotlin the name and type of the variable we are going to use, and the initialization sets the variable's first value:

And, normally we don't need to include a type when we initialize the variable, since Kotlin can figure it out on its own:

If you're having any trouble understanding variables, please review the previous lesson.


Discuss commenting and importance of good code commenting. Possibly show some examples from your own projects, or talk about a time that a comment really helped you out.

Before we continue, we need to introduce one new piece of Kotlin syntax: comments. Comments allow us to include descriptive text in our programs. Comments are completely ignored by the computer! They are entirely for the humans who write, read, and maintain the code.

Kotlin supports two types of comment:

Single-line comments are good when you have something short to say about something in your code. Multi-line comments that start with /* and end with */ are better when you have more to say and need to write one or more paragraphs.

Even though comments are ignored by the computer, they are essential to writing good computer code. We'll use comments frequently in our examples, and show you how you can use them to outline a plan for completing more complex programming tasks.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Modifying Variables

Now that we can store data in our programs we can harness the next fundamental computer superpower: math. Computers can do math, quickly and with (near) perfect accuracy. Many of the operations that we can perform on variables are mathematical in nature:

Go through the various mathematical operations that can be used to set variable values.

Variables can also be set based on the value of other variables:

Walk through how variables can be set by other variables. Show that changing the value of a variable after it has been used in an assignment does not change the value of the assigned variable.

Understanding Variable Assignment

Some of you, particularly those with a mathematical bent, may be alarmed by certain lines of Kotlin code. Like this one:

In math, the statement on the second line is non-sensical. Regardless of the value of i, it can never equal itself plus one—nonsense!

The source of the confusion here is the = symbol. In Kotlin, a single = does not indicate or test equality. It indicates assignment. We use it when we want to assign a value to a variable.

To further the confusion, assignments in Kotlin are evaluated from right to left. So they are neither math nor English! That's why the example above works.

Let's examine this assignment carefully together:

Explain how assignment works left to right and show some examples.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Self-Modification Shortcuts

It's fairly common in our programs to want to modify the value of a variable but start with its current value. For example:

These types of operations are common enough that there are a few shortcuts that you can use:

Similarly you can use -=, *=, and /=.

Finally, adding and subtracting by one are so common that they even have an additional set of shortcuts:

Similarly you can use ++ and --.

Binary Negation

What we've been covering in this lesson are known as operators. (We've covered at least a few of the ones listed in the linked reference. We'll get to more soon!) Many of them are for working with numbers, since, to a computer, everything is a number.

But there is one operator that we use to work with boolean values:

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.