As it was explained to me the conditions are evaulted from left to right.
Also it would make more since to evaulte the left side of the || first since if it is true that would be all it needed to enter into the condition.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main (void)
{
int x, y=100, z;
const int range=20, cutoff=1;
srand ( (unsigned) time(NULL));
while ( ((x=rand()%range)!=0) && ( (z=(y/x)) > cutoff) )
{
printf("%d\t%d\n",x, z);
}
printf("%d\t%d\n", x, z);
return 0;
}
As you can see the right side of the && isn't even evaulated when x is zero since the program is able that the condition is false and can leave the loop.
And an example using your conditions while some modifications to the functions. (note: The added () around the && conditions do not change it, was just a warning the compiler gave)
Code:
#include <stdio.h>
int fun1 (char str[], int i)
{
printf("Evaluating %s condition. ", str);
if ( i == 1)
{
printf("Returning false.\n");
return 0;
}
else
{
printf("Returning true.\n");
return 1;
}
}
int fun2 (char str[], int i)
{
printf("Evaluating %s condition. ", str);
if ( i != 1)
{
printf("Returning false.\n");
return 0;
}
else
{
printf("Returning true.\n");
return 1;
}
}
int main (void)
{
printf("First one: \n");
if (fun1("1st",1) || (fun1("2nd",2) && fun1("3rd",3)) );
printf("Second one: \n");
if (fun2("1st",1) || (fun2("2nd",2) && fun2("3rd",3)) );
return 0;
}