Reputation: 23
#include<stdio.h>
int main(){
int y=15;
if(y++>19 && y++!=21 && y++>21)
printf("%d",y);
else
printf("%d",y);
return 0;
}
I expected output to be 15 or even 18 but it gives out 16 and I don't know why.
Upvotes: 2
Views: 847
Reputation: 905
here is what you wrote but just a little bit shorter and faster
:
#include<stdio.h>
int main(){
int y = 15;
y++; // y = y + 1; means value of variable y equals 16 at this point
if(y != 21 && y > 19) // now you check this 16 != 21 && 16 > 19 which is false
printf("%d", y);
else // continue here directly
printf("%d", y); // print value of variable y
return 0; // exit success
}
Yes, the result is always 16. If this is not what you expected, then make sure you correct your first statement if(y != 21 && y > 19)
.
I hope, this will help you correct your IF-statement (my suggestion).
I can recommend you these following two videos to properly understand assignments, logical and comparison operators before writing such statements since:
false AND true -> false
true AND false -> false
(1) ArithmeticOperator Program https://www.youtube.com/watch?v=oaO2eT46Mm8
(2) ComparisonOperator Program https://www.youtube.com/watch?v=ovKVOj3xNsA
(3) LogicalOperator Program https://www.youtube.com/watch?v=kclf0PQnxrI
Upvote the answer after correction done.
Upvotes: 0
Reputation: 123598
Several things are going on in the line
if(y++>19 && y++!=21 && y++>21)
The &&
operator (along with the ||
, ?:
, and comma operators) forces left-to-right evaluation, meaning that y++ > 19
is evaluated before y++ != 21
, which will be evaluated before y++ > 21
. It also introduces a sequence point, so all side effects of y++ > 19
will be applied before y++ != 21
is evaluated.
The &&
operator short-circuits - y++ != 21
will only be evaluated if y++ > 19
evaluates to non-zero (true
), and y++ > 21
will only be evaluated if both prior expressions evaluate to non-zero.
The postfix ++
operator yields the current value of the operand (so y++
evaluates to 15
); as a side effect, it increments the operand, so after y++ > 19
is evaluated, y
is incremented to 16
, even though the overall expression evaluates to false
(0
).
Upvotes: 2
Reputation: 11174
I suggest to read about operator precedences (and while at it associativity). Usually each language has a specification that explains the functionings, so does C. Sources online are abound, a great book for learning C is K&R, well explained and interesting exercices.
I'll walk you through this example:
int y=15; // y is assigned the value 15, the return of the assignment is discarded (you will encounter that fact sooner or later surely)
The if statement can be described as (y++>19) && (y++!=21) && (y++>21)
to make it clear. The &&
operator is short-circuited: i.e. as soon as the final result can be determined, the rest will not be evaluated. Concretely in this case, as soon as a false
or 0
is seen it stops evaluating and return false
.
y++>19
has two operators, postfix increment (higher precedence) and greater than (lower precedence). The postfix increment returns the value first, and then increments it, so effectively 15>19 is evaluated and y
is incremented to 16
. Now remember the short-circuit. The program stops evaluating that expression since the final result will not change. The program enters the else part and prints the value assigned to y
which is 16
.
Upvotes: 2
Reputation: 73
Here y++>19 ==> y is compared against 19 and then incremented due to postfix ++. So, y++>19 is actually evaluated as 15>19 and then y gets incremented and becomes 16.
Since 15>19 is false, the rest of the condition y++!=21 && y++>21 is not evaluated and control goes to else condition and prints y. Since y already became 16 above, it prints 16.
Note that in short-circuit evaluation, A && B && C, if A gets evaluated to true then only B gets evaluated. If both A and B gets evaluated to true then only C gets evaluated.
Thus, in your case, since A is false, there was no further evaluation of B and C and the control just enters else part of it and prints y.
Upvotes: 3