4. Arithmetic Operations

Introduction

Here we will discuss another basic concept. Operators are how one manipulates data and values.

  1. Operators
  2. Operator Precedence and Associativity
  3. Compound Operators

Operators

Arithmetic Operators are used to perform mathematical calculations such as addition, subtraction, multiplication and division. There are 5 basic Arithmetic Operators in C++:

  1.     +         Add 2 operands (numbers)
  2.     –         Subtracts one operand from another
  3.     *        Multiplies one operand by another
  4.     /         Divides one operand by another
  5.     %       Divides two integers and returns the remainder

When both operands are integers, the result of the operation is an integer Hence 12/5=2. When one operand is a floating point number, the result is also a floating point number. Hence 12.0/5.0=2.4.
And % only works with integers.
For example the following code:

 include <iostream>
using namespace std;
int main()
{
double a=12, b=5;
int c=12, d=5;
cout << "----------- Double Operation -----------" << endl;
cout << a << "+" << b << "=" << a+b << endl;
cout << a << "-" << b << "=" << a-b << endl;
cout << a << "" << b << "=" << ab << endl;
cout << a << "/" << b << "=" << a/b << endl;
cout << "----------- Integer Operation -----------" << endl;
cout << a << "+" << b << "=" << a+b << endl;
cout << a << "-" << b << "=" << a-b << endl;
cout << a << "" << b << "=" << ab << endl;
cout << a << "/" << b << "=" << a/b << endl;
cout << a << "%" << b << "=" << a%b << endl;
system("pause");
return 0;
}

Outputs the following:

----------- Double Operation ----------
12+5=17
12-5=7
125=60 12/5=2.4 ----------- Integer Operation -----------
12+5=17
12-5=7
125=60
12/5=2
12%5=2

Operators Precedence and Associativity

Operator Precedence Rules determine which operation is to be evaluated first. For example 4+5*6. “*” has precedence over “+”. Hence *6 will be evaluated first.
Associativity Rules determin which operation is to be evaluated first if 2 or more operations have the same precedence. For example 3*7/2. “*” and “/” have the same precedence. Hence Evalutaion takes place according to the associativity rules, which is from left to right. 3*7 will be evaluated first.

OperatorPrecedenceAssociativity
Parenthesis ()High
* or / or %MediumLeft to Right
+ or –LowLeft to Right

Compound Opperators

OperatorExampleEquivalent Expression
+=index += 2;index = index + 2;
-=life -= 1;life life -1;
*=bonus *= increase;bonus = bonus * increase;
/=time / = hours;time = time / hours;
%=allowance %= 1000;allowance = allowance % 1000;

Straight-Forward isn’t it? To demonstrate the above, let us have the following example:

include <iostream>
using namespace std;
int main()
{
int a=10, b=20, c=30, d=40, e=50;
a+=1;
b-=1;
c*=3;
d/=4;
e%=5;
cout << "a\tb\tc\td\te\n << endl;
cout < a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl;
system("pause");
return 0;
}

Outputs the following:

a b c d e
11 19 90 10 0
Shopping Basket