Using Variables in Calculations

Now let’s talk about how we can actually use variables in our programs. A good place to start is by using our code to perform calculations.

Addition and Subtraction

#include <iostream>
 
int main() {
    int a = 5;
    int b = 6;
    int sum = a + b;
    std::cout << sum << "\n";

    int x = 50;
    int y = 100;
    std::cout << x + y << "\n";
    std::cout << x - y << "\n";

    return 0;
}

Multiplication and Division

#include <iostream>
 
int main() {
    int a = 5;
    int b = 6;
    std::cout << a * b << "\n";
    
    int x = 3;
    int y = 2;
    std::cout << x / y << "\n";

    int j = 3;
    float k = 2;
    std::cout << j / k << "\n";

    std::cout << 3 / 2 << "\n";
    std::cout << 3.0 / 2 << "\n";

    return 0;
}

By running the code above, you’ll see that division between two integers (3 and 2, in this case) results in an answer that is also an integer (doesn’t contain anything after the decimal point). In order to make 3 / 2 result in 1.5 (rather than 1), you need to ensure that at least one of the values in the division operation is a float or a double. If performing division just on numbers (like in lines 16 and 17), you could also just add a ".0" to the end of one of the numbers.

The Modulo Operation

One operation that may be unfamiliar to you is the modulo operation, which takes the remainder after dividing two numbers. To perform the modulo operation, just use the percent sign. For example:

  • 10 % 2 = 0 because 10 / 2 = 5 (remainder 0)
  • 10 % 3 = 1 because 10 / 3 = 3 (remainder 1)
  • 11 % 6 = 5 because 11 / 6 = 1 (remainder 5)

And here it is in code:

#include <iostream>
 
int main() {
    std::cout << 10 % 3 << "\n";
    return 0;
}

Some Arithmetic “Shortcuts”

Often when programming, you’ll want to modify a variable itself by a certain amount. For instance:

#include <iostream>
 
int main() {
    float dollars = 50.25;
    dollars = dollars - 10;
    std::cout << dollars << "\n";
    return 0;
}

C++ allows you to write this in fewer characters by replacing dollars = dollars - 10; with dollars -= 10;, which does the exact same thing. Similar operators exist for addition, multiplication, division, and modulo (+=, *=, /=, and %=)

You may also often need to increase or decrease a variable just by 1. This can easily be done using the increment and decrement operators (++ and --).

#include <iostream>
 
int main() {
    int ageInYears = 17;
    ageInYears++;
    ageInYears++;
    std::cout << ageInYears << "\n";

    int numberOfFish = 6;
    numberOfFish--;
    std::cout << numberOfFish << "\n";

    return 0;
}

These operators can actually be used in front of or behind variables, but there’s a subtle difference between the two options:

  • When used in front, the variable immediately increases or decreases by 1.
  • When used behind, the variable only increases or decreases by one at the end of the current “expression” (generally just the end of the current line).

Run the code below to see this difference a little more clearly:

#include <iostream>
 
int main() {
    int ageInYears = 17;
    std::cout << ageInYears++ << "\n";
    std::cout << ageInYears << "\n";

    int grade = 11;
    std::cout << ++grade << "\n";

    return 0;
}