• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Operator Precedence - Confused

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could you please help me understand whats happening here? Why does it print 0 and not 1?



Also, let

int a=1,b=5;

In the expression (a++ < b--) , does a++ and b-- happen first and then the 'less than' comparison?

Ta
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i++ is postfix notation.

So i is incremented only after the original value of i is used in the expression i=i++;

Hope that explains it...
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, evaluate the value of i. Store that on top of the stack. (0)
Next, increment i (through the post-fix operator). i now equals 1.
Now, perform the assignment. Assign i the value on top of the stack (0).

Now, i = 0.

Post-fix notation happens after the evaluation of the variable, but before the assignment.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the expression (a++ < b--) , does a++ and b-- happen first and then the 'less than' comparison?


Yes, but the comparison uses the original values of a and b.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, this is a terrible way to write code. As a rule of thumb, you should limit the number of side-effects (changes to values in variables) to one per line of code. I cannot think of any situation where "i=i++" would even be useful. Although, I could imagine where "a++ < b--" might be tempting, I would instead do something like this:

Of course, if the new values of a and b are needed in the body of the if statement, the increment and decrement would need to be inside the body rather than after it. However, the point is to separate these side-effects into separate statements. It will make the code much easier to read and maintain.

Keep Coding!

Layne
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic