[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 93

 
Share your experiences as to whether 1 or 2 is correct:
//--- 1.
if (cond_0)
{  if (cond_1) a=result_1;
   if (cond_2) a=result_2;
   if (cond_3) a=result_3;
}
//--- 2.
if (cond_0)
{       if (cond_1) a=result_1;
   else if (cond_2) a=result_2;
   else if (cond_3)  a=result_3;
}
 

Both are correct, but the second is quicker. The first always checks all conditions, the second does not check all conditions. And it's better to write it like this, so as not to confuse with else's affiliation:

//--- 2.
if (cond_0)
{  if (cond_1)           a=result_1;
   else if (cond_2)      a=result_2;
        else if (cond_3)  a=result_3;
}

Or put curly braces around the blocks at once.

 
Mathemat:

Both are correct, but the second is quicker. The first always checks all conditions, the second does not check all conditions. And it's better to write it like this, so as not to confuse with belonging else:

Or put curly braces around the blocks at once.

Thanks for the advice. While I used to strive to write my EA correctly in general, I now focus on its speed. :)
 
Mathemat:

Both are correct

No, the codes are not equal at all.
 
It might be easier to run it through swith() if the conditions don't need to be calculated...
 
TheXpert: No, the codes are not mutually exclusive at all.

Right, now let's speculate about what happens when cond_1, cond_2, cond_3 are not mutually exclusive...

I'm not arguing, the codes give different results in general. But if the conditions don't overlap, the results seem to be the same.

 
FAQ:
It may be easier to skip through swith(), if you don't need to calculate conditions...
In switch the value in case should be int, not always cond_ is int. Frankly speaking, I was interested in the logic itself, which entry is better/faster.
 
paladin80:
In switch the value at case should be int, not always cond_ is int. I was honestly interested in the logic itself, which entry is better/faster.
Separate conditions, with the most frequently used ones at the top
 
paladin80:
In switch the value at case must be int, not always cond_ is int. I was honestly interested in the logic itself, which entry is better/faster.

The second one, but there is a restriction on nesting.

It is better to use case, if there is such a possibility.

 
Yes, switch I use. As for mutually exclusive conditions, my understanding of the phenomenon is this:
//--- 1.
int x=1, y=1;
if (x==1)
{  if (y>0)  a=result_1;
   if (y<2)  a=result_2;
   if (y==1) a=result_3;
}
// a=result_3

//--- 2.
int x=1, y=1;
if (x==1)
{  if (y>0)            a=result_1;
   else if (y<2)       a=result_2;
        else if (y==1) a=result_3;
}
// a=result_1
Reason: