21 Operator Precedence.pptx - Topic 21 Operators and Operator Precedence 1 Prefix and Postfix Operators The operator or \u2013 can be used in two positions

# 21 Operator Precedence.pptx - Topic 21 Operators and...

• 11

This preview shows page 1 - 11 out of 11 pages.

Topic 21 Operators and Operator Precedence 1 Prefix and Postfix Operators The operator ++ or – can be used in two positions: prefix and postfix. Examples – prefix position: x = ++a; y = --b; Examples – postfix position: x = a++; y = b--; 2 Prefix OperatorsAssume following initial valuesabxy511??Statementsabxyx = ++a;6116? 3 Postfix OperatorsAssume following Initial valuesabxy511??Statementsabxyx = a++;6115? 4 Postfix and Prefix Operators Examples Example: a = 4; cout << "a is " << a << endl; cout << "a is " << a++ << endl; cout << "a is " << ++a << endl; Statements a Output a = 4; 4 a is 4 a is 4 a is 6 cout << "a is " << a << endl; 4 cout << "a is " << a++ << endl; 5 cout << "a is " << ++a << endl; 6 5 Operator Precedence Examples Example: Assume a is 3, b is 4, c is 5. -- a * (3 + b) / 2 – c++ * b      • • • 