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<<
operatorcin
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.
You can remember the direction of the arrows in an easy way
cout
- the text goes to the console -<<
pointing to thecout
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):
Operation | Meaning | |
---|---|---|
pub | a + b | Adds a and b |
pub | a - b | Subtracts b from a |
pub | a * b | Multiplies a and b |
pub | a / b | Divides the number a by b |
pub | a % b | Remainder of the division a by b |
These are not all math operators. We'll talk about the rest later.
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";
}
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:
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
(age: 20 => + 10 = 30)
(age: 20 => - 5 = 15)
(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:
#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";
}
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 calledage
of typeint
with no initial value. - When line
8
is executed, the program waits at this point until the user types something in and pressesEnter
.- Once the input is received,
std::cin
places the value it read intoage
. Since we typed in20
,age
is now20
.
- Once the input is received,
- When line
10
is executed,std::cout
prints out the value ofage
, which is still 20. - Line
11
changes the value to the result of20 + 30
, which is50
.age
now has a value of50
. - Finally, Line
12
is executed, which displays a value50
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 Operator | Equivalent To | |
---|---|---|
pub | a += b | a = a + b |
pub | a -= b | a = a - b |
pub | a *= b | a = a * b |
pub | a /= b | a = a / b |
And a presentation of these operators in action:
- Presentation
- Code
Below is a presentation of the program's operation, after entering the number 20
.
#include <iostream>
int main()
{
std::cout << "Enter some number: ";
int number;
std::cin >> number;
// Increase "number" by 5 and assign.
number += 5;
std::cout << "After increasing by 5: " << number << '\n';
// Decrease "number" by 10 and assign.
number -= 10;
std::cout << "Now, after decreasing by 10: " << number << '\n';
// Multiply by 3 and assign.
number *= 3;
std::cout << "Now, after multiplying by 3: " << number << '\n';
// Divide by 2 and assign.
number /= 2;
std::cout << "Now, after dividing by 2: " << number << '\n';
// Increase value by 1 (incrementation):
number++;
std::cout << "Now, after incrementation: " << number << '\n';
// Decrease value by 1 (decrementation):
number--;
std::cout << "Now, after decrementation: " << number << '\n';
}
You will learn more about the possibilities of variables in the coming lessons.