1

I have the following code which works as expected:

bool result=false;
for(int i=0;i<n;i++)
{
    if(face[i].intersect(*coordinates, org, dir))
    {
        result = true;
    }
}
return result;

But this one has a different behavior (the value pointed to by coordinates is different)

bool result=false;
for(int i=0;i<n;i++)
{
    result = result || faceList[i].intersect(*coordinates, org, dir, triangle);
}
return result;

What's the reason for this? Is the expression after || never evaluated if the one before is true? I thought this optimization was allowed only in an if statement.

galinette
  • 8,896
  • 2
  • 36
  • 87
  • 1
    Has nothing to do with an `if` statement, it has to do with the `||` operator itself. You *don't* get this behavior only if you make your own `operator||`. P.S. It evaluates left to right, so put the thing you always want evaluated on the left. – Mark Ransom Apr 20 '15 at 18:23
  • You compare apples with bananas (The reason to close it is wrong - but closing it is good). –  Apr 20 '15 at 18:24

0 Answers0