ASSOCIATIVITY AND PRECEDENCE OF INTEGER OPERATORS EXAMPLE


Operator
Associativity
++
postfix increment
right to left
--
postfix decrement
right to left
++
prefix increment
right to left
--
right to left
-
unary minus
right to left
*
multiplication
left to right
I
division
left to right
%
modulo
left to right
+
addition
left to right
-
subtraction
left to right
=
assignment
right to left
+=
add and assign
right to left
-=
subtract and assign
right to left
*=
multiply and assign
right to left
/=
divide and assign
right to left
%=
modulo and assign
right to left

// Associativity and Precedence of Int Op.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       float a, b, c, d, e, f, g, h;
       int d1, d2, d3;
       cout << "Enter the values for a,b,c,d,e,f,g; " << "\n";
       cin >> a >> b >> c >> d >> e >> f >> g;
       //Postfix increment
       cout <<"Posfix increment ++ operation" << "\n";
       cout << "The postfix operator increments i afther the variable i is used!!!" << "\n";
       a++;b++;c++;d++;e++;f++;g++;
       cout << "The value of a++ is:" << a << "\n" << "The value of b++ is:" << b << "\n" << "The value of c++ is:" << c <<
             "\n" << "The value of d++ is:" << d << "\n" << "The value of e++ is:" << e << "\n" << "The value of f++ is:" << f << "\n"
             << "The value of g++ is:" << g << "\n";
       //Postfix decrement
       cout << "The postfix operator -- decrements i afther the variable i is used!!!" << "\n";
       a--;b--;c--;d--;e--;f--;g--;
       cout << "The value of a-- is:" << a << "\n" << "The value of b-- is:" << b << "\n" << "The value of c-- is:" << c <<
             "\n" << "The value of d-- is:" << d << "\n" << "The value of e-- is:" << e << "\n" << "The value of f-- is:" << f << "\n"
             << "The value of g-- is:" << g << "\n";
       //Prefix increment
       cout <<"Prefix increment ++ operation" << "\n";
       ++a;++b;++c;++d;++e;++f;++g;
       cout << "The value of ++a is:" << a << "\n" << "The value of ++b is:" << b << "\n" << "The value of ++c is:" << c <<
             "\n" << "The value of ++d is:" << d << "\n" << "The value of ++e is:" << e << "\n" << "The value of ++f is:" << f << "\n"
             << "The value of ++g is:" << g << "\n";
       //Prefix Decrement
       cout << "Prefix decrement -- operation" << "\n";
       --a;--b;--c;--d;--e;--f;--g;
             cout << "The value of --a is:" << a << "\n" << "The value of --b is:" << b << "\n" << "The value of --c is:" << c <<
             "\n" << "The value of --d is:" << d << "\n" << "The value of --e is:" << e << "\n" << "The value of --f is:" << f << "\n"
             << "The value of --g is:" << g << "\n";
       //UNARY MINUS
       cout << " The use of unary minus" << "\n";
       -a;-b;-c;-d;-e;-f;-g;
       cout << "The value of -a is:" << a << "\n" << "The value of -b is:" << b << "\n" << "The value of -c is:" << c <<
             "\n" << "The value of -d is:" << d << "\n" << "The value of -e is:" << e << "\n" << "The value of -f is:" << f << "\n"
             << "The value of -g is:" << g << "\n";
       //MULTIPLICATION
       cout << "Use of multiplication " << "\n";
       cout << "MULTIPLICATION IS A BINARY OPERATOR" << "\n";
       a = b*c; b=a*c; c=a*b; d=a*c; e= a*b*d; f= d*a; g=a*b*c; h=b*c*a*g;
       cout << "The value of a = b*c is:" << a << "\n" << "The value of b=a*c is:" << b << "\n" << "The value of c=a*b is:" << c <<
             "\n" << "The value of d=a*c is:" << d << "\n" << "The value of e= a*b*d is:" << e << "\n" << "The value of f= d*a is:" << f << "\n"
             << "The value of g=a*b*c is:" << g << "\n"<< "The value of h=b*c*a*g is:" << h << "\n";
       //DIVISION
       cout << "Use of division" << "\n";
       a = b / c;
       cout << "The value of a = b/c is:" << a << "\n";
       //MODULO
       cout << "Use of MODULO %" << "\n";
       cout << "Enter the values for d1 and d2" << "\n";
       cin >> d1 >> d2;
       d3 = d2 % d1;
       cout << "The value of d3 = d2 % d1 is:" << d3 << "\n";
       //ADITION
       cout << "The basic operation of ADDITION" << "\n";
       a = b + c; d = e + a; h = a + b + c + d + e + f + g;
       cout << "The value of a = b + c is:" << a << "\n" << "The value of d = e + a is:" << d << "\n" << "The value of h = a + b + c + d + e + f + g is:" << h << "\n";
       //SUBTRACTION
       cout << "The basic operation of SUBTRACTION" << "\n";
       d = f - b;
       cout << "The value of d = f - b is: " << d << "\n";
       //ADD AND ASSIGN
       cout << "ADD AND ASSIGN +=" << "\n";
       a += b;
       cout << "The result of a += b is:" << "\n";
       //SUBTRACT AND ASSIGN
       cout << "SUBTRACT AND ASSIGN " << "\n";
       a -= b; d = -c;
       cout << "The result of a -= b is: " << a << "\n" << "The result of d -= c is" << d << "\n";
       //MULTIPLY AND ASSIGN
       cout << "MULTIPLY AND ASSIGN" << "\n";
       h *= a + b;
       cout << "The result of h *= a + b " << h << "\n";
       //DIVIDE AND ASSIGN
       cout << "DIVIDE AND ASSIGN /=" << "\n";
       d2 /= d1;
       cout << "The result of d2 /= d1 is: " << d2 << "\n";
       cout << "THE END" << "\n";
       system("pause");
       return 0;
}


The result is given below.

Share on Google Plus