Skip to main content

Operations on variables

In this lesson, you will learn to perform various operations with variables by:

  • Writing values to them
  • Modifying them with mathematical expressions
  • Displaying their contents in the console

You will also learn how to enable simple user interaction with the program.

User interaction​

Our programs become more interesting when we let the user interact with them. We will do this by adding a new instruction using cin (character input):

#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "You're " << age << " years old.\n";
}

cin allows us to save the content of the so-called "standard input" (stdin) to the variable that is passed after the >> characters. This allows the user to type something into the console, and then that value is placed into a variable.

Please note that:

  • cout uses the << operator
  • cin uses the >> operator

In addition, you can see that we didn't assign an initial value to age. The initial value is not needed because we use std::cin immediately after we create the variable, which assings a value to the variable. After which, the variable is now initialized.

Remember

You can remember the direction of the arrows in an easy way

  • cout - the text goes to the console - << pointing to the cout
  • cin - the text goes to the variable - >> pointing to the variable

Math operations​

Variables that store numbers can be freely modified using typical mathematical notation (operators):

OperationMeaning
a + bAdds a and b
a - bSubtracts b from a
a * bMultiplies a and b
a / bDivides the number a by b
a % bRemainder of the division a by b
note

These are not all math operators. We'll talk about the rest later.

caution

The ^ symbol is not exponentiation!

Let's see these operators in practice:

#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "In 10 years, you'll be " << (age + 10) << " years old\n";
std::cout << "5 years ago, you were " << (age - 5) << " years old\n";
std::cout << "When you are twice as old, you'll be " << (age * 2) << " years old\n";
std::cout << "Someone twice as young is " << (age / 2) << " years old\n";
std::cout << (age % 10) << " years ago your age was divisible by 10";
}
Alignment

Note that it doesn't matter whether you use Space or Tab to align code.

None of the aforementioned operators will affect the variable they're used on. The content of age does not change.

By entering the age 20, we will get the following result:

Console
In 10 years, you'll be 30 years old
5 years ago, you were 15 years old
When you are twice as old, you'll be 40 years old
Someone twice as young is 10 years old
0 years ago your age was divisible by 10
Description
(age: 20 => + 10 = 30)
(age: 20 => - 5 = 30)
(age: 20 => * 2 = 40)
(age: 20 => / 2 = 10)
(age: 20 => % 10 = 0)

As we can see, the value of the variable age remained the same at each step (keeping a value of 20 the entire time).
This is because in the notation:

a + b

The a + b expression results in some value and the contents of the variables are left untouched.

Modifying variables​

Often times, we will want to change a variable's value by giving it a new one. You can simply use the assignment operator = again on a variable to give it a new value.

In the following example, the variable age is initially created with a value of 20. Then, we assign a new value of 37 to it.

int age = 20;
age = 37;

If, for example, we want to increase a variable by 10, we have to assign the result of an addition expression like so:

age = age + 10;

As this pattern is so common, C++ provides a shorthand syntax for this:

age += 10;

And you can see how it works in the example below:

🔹 main.cpp
#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "You're now " << age << " years old\n";
age += 30;
std::cout << "In 30 years, you'll be " << age << " years old";
}
Result
You're now 20 years old
In 30 years, you'll be 50 years old

Let's analyze the steps this program took in order. Consider the variable age:

  • Line 7 defines a variable called age of type int with no initial value.
  • When line 8 is executed, the program waits at this point until the user types something in and presses Enter.
    • Once the input is received, std::cin places the value it read into age. Since we typed in 20, age is now 20.
  • When line 10 is executed, std::cout prints out the value of age, which is still 20.
  • Line 11 changes the value to the result of 20 + 30, which is 50. age now has a value of 50.
  • Finally, Line 12 is executed, which displays a value 50

In addition to the operator += presented above, there are many other operators available that modify the content of variables; here are the most common ones:

Shorthand OperatorEquivalent To
a += ba = a + b
a -= ba = a - b
a *= ba = a * b
a /= ba = a / b

And a presentation of these operators in action:

Below is a presentation of the program's operation, after entering the number 20.

You will learn more about the possibilities of variables in the coming lessons.

Operations on variables

In this lesson, you will learn to perform various operations with variables by:

  • Writing values to them
  • Modifying them with mathematical expressions
  • Displaying their contents in the console

You will also learn how to enable simple user interaction with the program.

User interaction​

Our programs become more interesting when we let the user interact with them. We will do this by adding a new instruction using cin (character input):

#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "You're " << age << " years old.\n";
}

cin allows us to save the content of the so-called "standard input" (stdin) to the variable that is passed after the >> characters. This allows the user to type something into the console, and then that value is placed into a variable.

Please note that:

  • cout uses the << operator
  • cin uses the >> operator

In addition, you can see that we didn't assign an initial value to age. The initial value is not needed because we use std::cin immediately after we create the variable, which assings a value to the variable. After which, the variable is now initialized.

Remember

You can remember the direction of the arrows in an easy way

  • cout - the text goes to the console - << pointing to the cout
  • cin - the text goes to the variable - >> pointing to the variable

Math operations​

Variables that store numbers can be freely modified using typical mathematical notation (operators):

OperationMeaning
a + bAdds a and b
a - bSubtracts b from a
a * bMultiplies a and b
a / bDivides the number a by b
a % bRemainder of the division a by b
note

These are not all math operators. We'll talk about the rest later.

caution

The ^ symbol is not exponentiation!

Let's see these operators in practice:

#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "In 10 years, you'll be " << (age + 10) << " years old\n";
std::cout << "5 years ago, you were " << (age - 5) << " years old\n";
std::cout << "When you are twice as old, you'll be " << (age * 2) << " years old\n";
std::cout << "Someone twice as young is " << (age / 2) << " years old\n";
std::cout << (age % 10) << " years ago your age was divisible by 10";
}
Alignment

Note that it doesn't matter whether you use Space or Tab to align code.

None of the aforementioned operators will affect the variable they're used on. The content of age does not change.

By entering the age 20, we will get the following result:

Console
In 10 years, you'll be 30 years old
5 years ago, you were 15 years old
When you are twice as old, you'll be 40 years old
Someone twice as young is 10 years old
0 years ago your age was divisible by 10
Description
(age: 20 => + 10 = 30)
(age: 20 => - 5 = 30)
(age: 20 => * 2 = 40)
(age: 20 => / 2 = 10)
(age: 20 => % 10 = 0)

As we can see, the value of the variable age remained the same at each step (keeping a value of 20 the entire time).
This is because in the notation:

a + b

The a + b expression results in some value and the contents of the variables are left untouched.

Modifying variables​

Often times, we will want to change a variable's value by giving it a new one. You can simply use the assignment operator = again on a variable to give it a new value.

In the following example, the variable age is initially created with a value of 20. Then, we assign a new value of 37 to it.

int age = 20;
age = 37;

If, for example, we want to increase a variable by 10, we have to assign the result of an addition expression like so:

age = age + 10;

As this pattern is so common, C++ provides a shorthand syntax for this:

age += 10;

And you can see how it works in the example below:

🔹 main.cpp
#include <iostream>

int main()
{
std::cout << "Please enter your age: ";

int age;
std::cin >> age;

std::cout << "You're now " << age << " years old\n";
age += 30;
std::cout << "In 30 years, you'll be " << age << " years old";
}
Result
You're now 20 years old
In 30 years, you'll be 50 years old

Let's analyze the steps this program took in order. Consider the variable age:

  • Line 7 defines a variable called age of type int with no initial value.
  • When line 8 is executed, the program waits at this point until the user types something in and presses Enter.
    • Once the input is received, std::cin places the value it read into age. Since we typed in 20, age is now 20.
  • When line 10 is executed, std::cout prints out the value of age, which is still 20.
  • Line 11 changes the value to the result of 20 + 30, which is 50. age now has a value of 50.
  • Finally, Line 12 is executed, which displays a value 50

In addition to the operator += presented above, there are many other operators available that modify the content of variables; here are the most common ones:

Shorthand OperatorEquivalent To
a += ba = a + b
a -= ba = a - b
a *= ba = a * b
a /= ba = a / b

And a presentation of these operators in action:

Below is a presentation of the program's operation, after entering the number 20.

You will learn more about the possibilities of variables in the coming lessons.