wood burning stoves 2.0*
The moose likes C / C++ and the fly likes problems with expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » C / C++
Bookmark "problems with expressions" Watch "problems with expressions" New topic
Author

problems with expressions

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43

The output of the above code is -2 2 0 1
Why is this happening?I guess it should have been -2 3 1 1
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


++i is true, so the rest will not be evaluated. ++j, ++k will not be executed.


[My Blog]
All roads lead to JavaRanch
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Expressions are evaluated from left to right in c++,
As ++i is true, right hand side of || will not get evaluated.


Sage of The Monstrous Toad of Mount Myoboku
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
@rohan yadav,@Christophe Verré thanks...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
rohan yadav wrote:Expressions are evaluated from left to right in c++ . . .
Is that a general rule, or does it only apply to expressions separated by && and ||?
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
I guess Left to right is general rule but i am not sure, I tried some generalized scenario's, i find left to right rule working properly,
But as i said i am not sure...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
You would need to check the language specifications carefully; I know left-to-right is specified for Java, but I am not sure about C++.
 
GeeCON Prague 2014
 
subject: problems with expressions