Operator precedence with value change within expression – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1784 siteicon   siteicon   siteicon  

Operator precedence with value change within expression

Learning operator precedence case when value changes while evaluating the expression.

Learning Objectives

  • Learning to deduce the precedence when a variable value changes while in the expression building itself, like for pre-increment, decrement cases.

 

Source Code

TC++ #1784

Source Code

Run Output

14
-9

Code Understanding

int x=2,y=5; 
int n=x – –y + y/x * (x*y++);
Let us build this formula one by one following the precedence rules and replacing the variables with given values of x and y.

2 – 4 + 4/2 * (2*4)         //we write it from left to right. first y is decremented to 4 which value is subsequently used for other places of y

2 -4 + 4/2 * 8
2-4+ 2 * 8
2-4+16                            //we reduce it to a level of addition and subtractions by first solving bracket and then division and then multiplication

2-4+16 = 14                  //This is final deduction

int n1=–x – (y + y/x);
Since previous statement had last y as y++ so in this statement incremented value of y would be used. this would be 5 as earlier y was decremented from 5 to 4 and here it would be again incremented to 5. Now making further deductions with precedence rules.

1 – (5 + 5/1)        //solving left to right x will decremented first and subsequent x will be same decremented value which is 1.

1 – (5 + 5)   //Here we try to work the bracket first followed by division

1 – 10   = – 9      // We come to final deduction level here.

 

Notes

  • Some older compilers follow different inline deduction rules. For example they may not be able to update prefix operator changes while building the formula. So the exact answers may be different. For example the answer for above problem in dos based turboc++ compilers would be different. This anomaly can only be removed by using latest c++ standards from c++ 2011 onwards.


Suggested Filename(s): op-prec-valchange.cpp



Share

sunmitra| Created: 19-Dec-2017 | Updated: 31-Aug-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back