Operators (Part 1)

Previous Index Next

Unary and binary operators Parentheses Arithmetic operators

Unary and binary operators Top

A unary operator is an operator which requires one operand only. For example, the negative sign (-) in the number "-10" is a unary operator.

A binary operator is an operator which requires two operands, one on the left and the other on the right.


Parentheses Top

When writing expressions, parentheses () (singular: parenthesis) are used to group expressions which are to be performed first, or just for increasing readability of the program.


Arithmetic operators Top

The following table shows five arithmetic operators (addition, subtraction, multiplication, division and modulus). They are all binary operators.

Operator in C++ Operation Sample C++ expression Equivalent Pascal expression
+ Addition p + q p + q
- Subtraction m - n m - n
* Multiplication a * b a * b
/ (See Program 3) Division x / y x div y (if both x and y are of integer types)
x / y (if at least one operand is of floating point type)
% Modulus r % s r mod s

Notes

  1. The data type of the result of addition, subtraction, multiplication and division operations depends on its operands:
    1. If both operands are of integer types, the result will be of integer type.
    2. If at least one of the operands is of floating-point type, the result will be a floating-point number.
  2. The following program demonstrates how the data types of the operands affect the results of division:

    Program 3: Division in C++
    #include <iostream.h>

    main()
    {
      int i = 17, j = 5;
      float x = 17, y, z;

      cout <<
    i / j << endl;         // 3 (NOT 3.4) will be displayed.

      // Note that i & j are of type int while x is of type float.
     
    y = i / j;                    // 3 will be stored in y.
      cout << y << endl;
     
    z = x / j;                    // 3.4 will be stored in z.
      cout << z << endl;

      // Compare the following:
      cout <<
    18 / 8 << endl;       // 2 will be displayed.
      cout <<
    18.0 / 8 << endl;     // 2.25 will be displayed.
      cout <<
    18 / 8.0 << endl;     // 2.25 will be displayed.
      cout <<
    18.0 / 8.0 << endl;   // 2.25 will be displayed.

      return 0;
    }
  3. The addition, subtraction, multiplication and modulus operations are similar to those in Pascal. For example:
    C++ expression Equivalent Pascal expression
    (x + y - a) % b (x + y - a) mod b
  4. The operands in a modulus operation must be of integer types.
  5. Character values can also be used in arithmetic expressions. In this case, the ASCII code of the character will be used for calculation. See the following program:
    Program 4: Using characters in arithmetic expressions
    #include <iostream.h>

    main()
    {
      char c1 = 'A',        // The ASCII code of 'A' is 65.
           c2;
      int i;
      float x;

     
    c2 = c1 + 5;          // 'F' (ASCII code = 65+5 = 70) is stored in c2.
      cout << c2 << endl;

      x = -0.25;
     
    i = c2 * x;           // -17 [integer part of 70*(-0.25) = -17.5] is ...
      cout << i << endl;    // ... stored in i.

      return 0;
    }

Previous Index Next