• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems with expressions

 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

++i is true, so the rest will not be evaluated. ++j, ++k will not be executed.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expressions are evaluated from left to right in c++,
As ++i is true, right hand side of || will not get evaluated.
 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@rohan yadav,@Christophe Verré thanks...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic