Arithmetic Operators
Arithmetic Operators
Operators
You can also add two variables together. Modify the code to look like what’s
below and then click the TRY IT button again.
int a = 7;
int b = 3;
cout << a + b << endl;
challenge
IMPORTANT
You may have noticed that when you add an int of 3 to a double of 7.1
you get 10.1. However, when you add an int of 3 to a double of 7.0, you
get 10 instead of 10.0. This occurs because by default cout does not
print zeros after a decimal point unless those zeros are enclosed by
other non-zero digits.
Examples:
* cout << 7 + 3.14; prints 10.14
* cout << 7.0 + 3.00; prints 10
* cout << 7.00 + 3.01400; prints 10.014
==Note== that when an int and a double are added together, the result
will be a double because the program will take on the data type that is
more flexible.
Printing Floating Point Numbers
cout
The cout command is considered to be non-specific because you can use the
same syntax for all of your printing needs (e.g. cout << 1;, cout <<
"Hello";, and cout << true;). However, for printing certain numbers, it is
not always clear if what’s printed is an int or a double sometimes.
int a = 1;
double b = 1.0;
cout << a << endl;
cout << b << endl;
Even though you are printing a double of 1.0, the system will disregard the
decimal and the trailing zero when cout is used. There is another print
command called printf() that also prints text to the console.
printf()
printf() originates from the C language and, unlike the cout command, it
is considered to be specific. This means that you must specify what type of
data you want to print before you can use the command successfully.
int a = 1;
double b = 1.0;
cout << a << endl;
cout << b << endl;
printf("%d \n", a);
printf("%f \n", b);
challenge
important
IMPORTANT
When printf() is used, a specifier is needed in order to tell the system
what type of data you want to print. The %d tells the system to print an
integer and %f tells the system to print a floating point number. If you
use an incorrect specifier, you will receive an error message. By
default, floating point numbers contain six zeros after the decimal
point if they are printed using printf().
Incrementing Variables
Incrementing a variable means to increase the value of a variable by a set
amount. The most common incrementation you will see is when a variable
increments itself by the value of 1.
int a = 0;
a = a + 1;
cout << a << endl;
How to Read a = a + 1
The variable a appears twice on the same line of code. But each instance of
a refers to something different.
.guides/img/Increment
int a = 0;
int b = 0;
a = a + 1;
b++;
cout << a << endl;
cout << b << endl;
In the cases where you need to increment by a different number, you can
specify it by using the += operator. You can replace b++; with b+=1; in the
code above and still get the same result.
challenge
String Concatenation
String concatenation is the act of combining two strings together. This is
done with the + operator.
challenge
Subtraction
Copy the code below and TRY IT.
int a = 10;
int b = 3;
int c = a - b;
cout << c << endl;
challenge
important
IMPORTANT
Did you notice that you were able to subtract a bool from an int?
Recall that a bool of true is actually an integer of 1 and false is actually
0. Thus, the system is able to add and subtract bools and ints. In
addition, assigning b which is of type int to 3.1 will force the variable
to adopt the integer value of 3 instead. Remember that all ints
disregard decimal places.
Like +=, there is a shorthand for decrementing a variable, -=. For example,
if you want to decrement the variable a by 2 instead of 1, replace a-- with
a-=2.
Division
Division in C++ is done with the / operator.
double a = 25.0;
double b = 4.0;
printf("%f \n", a / b);
challenge
double a = 25.0;
double b = 4.0;
a /= b;
printf("%f \n", a);
Hint(s)
/= works similarly to += and -=.
important
IMPORTANT
Division by zero is undefined in mathematics. In C++, dividing by an
integer of 0 results in an error message. However, dividing by a
double of 0.0 results in inf which is short for infinity.
Integer Division
Normally, you use double in C++ division since the result usually involves
decimals. If you use integers, the division operator returns an int. This
“integer division” does not round up, nor round down. It removes the
decimal value from the answer.
.guides/img/IntDivision
int a = 5;
int b = 2;
cout << a / b << endl;
Modulo
Modulo
Modulo is the mathematical operation that performs division but returns
the remainder. The modulo operator is %.
.guides/img/Modulo
int modulo = 5 % 2;
cout << modulo << endl;
challenge
Multiplication
C++ uses the * operator for multiplication.
int a = 5;
int b = 10;
cout << a * b << endl;
challenge
int a = 5;
int b = 10;
a*=b;
cout << a << endl;
Hint(s)
*= works similarly to +=, -=, and /=.
Order of Operations
Order of Operations
C++ uses the PEMDAS method for determining order of operations.
.guides/img/PEMDAS
int a = 2;
int b = 3;
int c = 4;
double result = 3 * a - 2 / (b + 5) + c;
printf("%f \n", result);
Explanation
challenge
Mental Math
5 + 7 - 10 * 4 / 2
Solution
-8
5 * 8 - 7 % 2 - 18 * -1
Solution
57
9 / 3 + (100 % 100) - 3
Solution
0
12 - 2 * pow(2, 3) / (4 + 4)
Solution
10
Type Casting
Type Casting
Type casting (or type conversion) is when you change the data type of a
variable.
More information
If either or both numbers in C++ division are doubles, then double division
will occur. In the last example, numerator and denominator are both ints
when the division takes place which results in an int of 1. An integer of 1
converted to a double is 1.000000 but cout removes the decimal point and
all of the trailing zeros.
int a = 5;
string b = "3";
cout << a + b << endl;
In C++, you can add a combination of ints, doubles, and bools together.
Remember that a boolean is either 1 if it’s true or 0 if it’s false. In the
example above, adding a string to an integer results in an error. That’s
because a string has no numerical value and can only be added to other
strings. However, you can convert the string b to an integer to fix the
problem by using stoi(). stoi() acts as a function to convert a string into
an integer. The string or string variable goes into the () to be converted.
See below for a list of type-conversion functions.
int a = 5;
string b = "3";
string c = "3.14";
bool d = true;
cout << a + stoi(b) << endl;
Output
Function Input Type Example
Type
stoi() string int stoi(“10”)
challenge
important
IMPORTANT
You can convert the string "3.14" to an integer using stoi() which will
result in an int of 3. To retain the decimal places, use stod() instead. In
addition, the to_string() function will convert a boolean into the
string form of its numerical value. to_string(true) will convert true to
"1" instead of 1. This is why adding b, which is a string of "3", to
to_string(d) resulted in the string of "31".
Formative Assessment 1
Formative Assessment 2