Free Hosting

Arithmetic operators


Unary arithmetic operators
There are two unary arithmetic operators, plus (+), and minus (-). If you remember, unary operators are operators that only take one operand.
OperatorSymbolFormOperation
Unary plus++xValue of x
Unary minus--xNegation of x
The unary plus operator returns the value of the operand. In other words, +5 = 5, and +x = x.
The unary minus operator returns the operand multiplied by -1. In other words, if x = 5, -x = -5.
For best effect, both of these operators should be placed immediately preceding the operand (eg. -x, not - x).
Do not confuse the unary minus operator with the binary subtraction operator, which uses the same symbol. For example, in the expression x = 5 - -3;, the first minus is the subtraction operator, and the second is the unary minus operator.
Binary arithmetic operators
There are 5 binary arithmetic operators. Binary operators are operators that take a left and right operand.
OperatorSymbolFormOperation
Addition+x + yx plus y
Subtraction-x – yx minus y
Multiplication*x * yx multiplied by y
Division/x / yx divided by y
Modulus (Remainder)%x % yThe remainder of x divided by y
Of these operators, the only ones that really need further explanation are division and modulus (remainder).
Integer and floating point division
It is easiest to think of the division operator as having two different “modes”. If both of the operands are integers, the division operator performs integer division. Integer division drops any fractions and returns an integer value. For example, 7 / 3 = 2 because the fraction is dropped. Note that integer division does not round. For example, 3 / 4 = 0, not 1.
If either or both of the operands are floating point values, the division operator performs floating point division. Floating point division returns a floating point value, and the fraction is kept. For example, 7.0 / 3 = 2.333, 7 / 3.0 = 2.333, and 7.0 / 3.0 = 2.333.
Note that trying to divide by 0 (or 0.0) will generally cause your program to crash, as the result are undefined!
Modulus (remainer)
The modulus operator is also informally known as the remainder operator. The modulus operator only works on integer operands, and it returns the remainder after doing integer division. For example, 7 / 3 = 2 remainder 1, thus 7 % 3 = 1. As another example, 25 / 7 = 3 remainder 4, thus 25 % 7 = 4.
Modulus is very useful for testing whether a number is evenly divisible by another number: if x % y == 0, then we know that y divides evenly into x. This can be useful when trying to do something every nth iteration.
For example, say we wanted to write a program that printed every number from 1 to 100 with 20 numbers per line. We could use the modulus operator to determine where to do the line breaks. Even though you haven’t seen a while statement yet, the following program should be pretty comprehensible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
int main()
{
    using namespace std;
 
    // nCount holds the current number to print
    int nCount = 1; // start at 1
 
    // Loop continually until we pass number 100
    while (nCount <= 100)
    {
        cout << nCount << " "; // print the current number
 
        // if nCount is divisible by 20, print a new line
        if (nCount % 20 == 0)
            cout << endl;
 
        nCount = nCount + 1; // go to next number
    } // end of while
} // end of main()
A warning about integer division and modulus with negative numbers
If either or both operands of integer division are negative, the compiler is free to truncate up or down! Most modern compilers round towards 0. For example, -5 / 2 can evaluate to either -3 or -2, depending on whether the compiler rounds down or rounds toward 0.
If either operand of the modulus operator is negative, the results of the modulus can be either negative or positive! For example, -5 % 2 can evaluate to either 1 or -1.
Arithmetic assignment operators
OperatorSymbolFormOperation
Assignment=x = yAssign value y to x
Addition assignment+=x += yAdd y to x
Subtraction assignment-=x -= ySubtract y from x
Multiplication assignment*=x *= yMultiply x by y
Division assignment/=x /= yDivide x by y
Modulus assignment%=x %= yPut the remainder of x / y in x
By now, you should already be well acquainted with the assignment operator, so no need to discuss it here. Because writing statements such as x = x + y is so common, C++ provides 5 arithmetic assignment operators for convenience. For example, instead of writing x = x + y, you can write x += y. Instead of x = x * 5, you can write x *= 5.
Quiz
1) What does the following expression evaluate to? 6 + 5 * 4 % 3
2) Write a function called IsEven() that returns true if an integer passed to it is even. Use the modulus operator to test whether the integer parameter is even.
3) Name two things to watch out for when using integer division.
Answers

0 comments:

Blogger Template by Clairvo