In my if
statement, the first condition for &&
is 0
(false), so the expression 0 && (a++)
is equal to 0
, right? Then 0==0
it should be true. Why am I getting else
here? Please explain!
int a=0;
if(0 && (a++)==0)
{
printf("Inside if");
}
else
{
printf("Else");
}
printf("%i",a);
The Definition of a Zero Factorial This still counts as a way of arranging it, so by definition, a zero factorial is equal to one, just as 1! is equal to one because there is only a single possible arrangement of this data set.
Factorial of a number in mathematics is the product of all the positive numbers less than or equal to a number. But there are no positive values less than zero so the data set cannot be arranged which counts as the possible combination of how data can be arranged (it cannot). Thus, 0! = 1.
Zero is the integer denoted 0 that, when used as a counting number, means that no objects are present. It is the only integer (and, in fact, the only real number) that is neither negative nor positive. A number which is not zero is said to be nonzero. A root of a function is also sometimes known as "a zero of ."
The ==
operator has a higher priority than the &&
operator, so this line:
if(0 && (a++)==0)
is treated like this:
if( 0 && ((a++)==0) )
So the whole expression under the if
is false, and a++
is not even evaluated due to short circuitry of the &&
operator.
You can read about Operator Precedence and Associativity on cppreference.com.
When in doubt, you should use parenthesis to express your intention clearly. In this case, it should be:
if( (0 && (a++)) == 0 )
Though, it does not make any sense, as it always evaluates to true
and a++
is not incremented here, either.
As already mentioned, the precedence of ==
is higher than precedence of &&
, so the statement is resolved into
if( 0 && ((a++)==0))
However, still even if you add the correct order of brackets, a++
returns the original value of a
, which is 0
, but the a
is incremented. If you want to return the updated value of a
, you should write ++a
if( ((++a) && 0) == 0 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With