Operators (Part 2)

Previous Index Next

Cast operators More assignment operators Multiple assignments Increment and decrement operators

Cast operators Top

A cast operator temporarily changes the data type of the operand (following the cast operator). It is formed by placing parentheses around a data type name. It is a unary operator, and the operands is on the right of the operator.

For example, suppose x is an integer variable. The expression "(float) x" temporarily treats x as a floating-point variable in the expression.

The following program shows some examples how cast operators are used:

Program 5:Using cast operators
#include <iostream.h>

main()
{
  int a = 17, b = 5;
  float x, y;
  char ch = 'A';

  // Compare the following:
  cout <<
(float) a / b << endl;         // 3.4 will be displayed.
  cout << a / b << endl;                 // 3 will be displayed.

  x = a /
(float) b;                     // 3.4 will be stored in x.
  y = a / b;                             // 3 will be stored in y.
  cout << "x=" << x << " y=" << y << endl;

  cout << ch << endl;                    // 'A' will be displayed.
  cout <<
(int) ch << endl;              // 65 (ASCII of 'A') will be displayed.

  return 0;
}

More assignment operators Top

In addition to "=", there are ten more assignment operators. Five of them are shown below:

Assignment operator Sample expression Explanation
+= a += 7 a = a + 7
-= b -= 3 b = b - 3
*= c *= 2 c = c * 2
/= d /= 5 d = d / 5
%= e %= 4 e = e % 4

All assignment operators are binary operators.

In general, any statement of the form
  <variable> <operator>= <expression>;
where <operator> is one of "+", "-", "*", "/" or "%", can be written in the form
  <variable> = (<variable>) <operator> (<expression>);

The following program makes use of these assignment operators:

Program 6:Using special assignment operators
#include <iostream.h>

main()
{
  int a, b;
  char c;

  a = 14;
 
a -= 3;                // 11 will be assigned to a.
  cout << a << endl;

  b = 7;
 
b += a * 3;            // 7 + 11 * 3 = 40 will be assigned to b.
  cout << b << endl;

  c = 'A';
 
c += 3;                // 'D' will be assigned to c.
  cout << c << endl;

  return 0;
}

Multiple assignments Top

In most programming languages (including Pascal), one statement has to be used to perform one assignment operation, and the assignment operation does not return anything. However, in C++, an assignment operation can be an expression, and can return value. Therefore a statement can perform multiple assignments. See the following program:

Program 7: Multiple assignments
include <iostream.h>

main()
{
  int a, b, c, d, e;

  // 3 is assigned to b, and then 3 is assigned to a.

  a = b = 3;

  // 3*7 = 21 is assigned to b, and then 21 is assigned to c.

  c = b *= 7;

  // 8 is assigned to d and then 21+8 = 29 is assigned to c.
 
c += d = 8;

  // 3+1 = 4 is assigned to a, and then 7*4 = 28 is assigned to e.
 
e = 7 * (a += 1);

  cout << "a\tb\tc\td\te\n";
  cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl;

  return 0;
}

a = b = 3;
Assignments are performed from right to left in a multiple assignment statement. Therefore in this statement, "b = 3" is performed first, and this expression returns the value 3. Then the assignment "a = 3" is performed.

c = b *= 7;
In this statement, "b *= 7" is performed first. The value stored in b becomes 3*7=21, and this is the returned value. Then the assignment "c = 21" is performed.

c += d = 8;
In this statement, "d = 8" is performed first, and the returned value is 8. Then the assignment "c += 8" is performed, i.e. 21+8=29 is stored in c.

e = 7 * (a += 1);
The precedence of assignment operation is lower than multiplication. Therefore parentheses are necessary in this statement. After the assignment operation "a += 1", the value 3+1=4 is stored in a, and the returned value is 4. Finally, "e = 7 * 4" is performed, and 28 is stored in e.


Increment and decrement operators Top

C++ provides two types of increment operators and two types of decrement operators:

Operator Name of operator Sample expression Explanation
++<operand> Preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides.
<operand>++ Postincrement b++ Use the current value of b in the expression in which b resides, then increment b by 1.
--<operand> Predecrement --c Decrement c by 1, then use the new value of c in the expression in which c resides.
<operand>-- Postdecrement d-- Use the current value of d in the expression in which d resides, then decrement d by 1.

All increment and decrement operators are unary operators. As seen from the above table, preincrement and predecrement operators have their operand on their right, while postincrement and postdecrement operators have their operand on their left.

The following program shows the differences between preincrement and postincrement operators. Note the properties shown applies to predecrement and postdecrement operators.

Program 8: Using increment operators
#include <iostream.h>

main()
{
  int i = 1,    // i is initialised to 1.
      j;

  cout << i << endl;

  // "i++" and "++i" have no observable differences when they are used as
  // statements by themselves (not part of an expression).
 
i++;          // i is incremented to 2.
  cout << i << endl;
 
++i;          // i is incremented to 3.
  cout << i << endl;

 
j = i++;      // j is assigned to 3 and then i is incremented to 4.
  cout << "i=" << i << ", j=" << j << endl;
 
j = ++i;      // i is incremented to 5 and then j is assigned to 5.
  cout << "i=" << i << ", j=" << j << endl;

  return 0;
}

These operators also apply to character variables and floating-point variables.

Previous Index Next