In school we're to give a test in a couple of days where the questions are mainly gonna be on Expression evaluation stuff.
I'm particularly confused about how some of the expressions get evaluated.
This one for example:
when i, j, k and m are printed, the output is -2, 3, 0, 1 respectively.Code:int i = -3, j = 2, k = 0 m = ++i && ++j || ++k;
Now my question is why doesn't K get incremented?
and how on earth are such expressions evaluated? In what order etc.
Please be kind enough to explain this to me.
thanks
It runs through ++i && ++j which is obviously true so it never gets to the or?
^ ++i would be -2 and ++ j would be 3 {right?}
then can you please tell me how -2&&3 is evaluated? {bitwise?}
shouldn't the( m = ++i && ++j || ++k;) ===>( m == ++i && ++j || ++k;)
Last edited by TkTech; 10-18-2009 at 05:48 AM. Reason: Smilies
What you're running into is lazy evaluation of logical operators.Code:int i = -3, j = 2, k = 0 m = ++i && ++j || ++k;
++i increments i and returns -2
&& interprets that left-hand value as true, and requires the right-hand value to return a value so...
++j increments j and returns 3
&& interprets 3 as true. true and true is true
|| takes the result of true, and doesn't need to process ++k to know that it will return true (or 1) to m. As a result, ++k never gets processed.
^ is it that simple?
I was thinking of all the precedence involved and if there would be any issues in the evaluation of {++i && ++J} because of i's value {-2}.
Btw, in case the situation were to be reversed:
then the value of i would be -2, j =3 and k = 1. is that correct?Code:int i = -3, j = 2, k = 0 m = ++i || ++j && ++k;
{sorry for being such a nag... i'm only trying to get hold of the basics}
&& always treats non-zero numbers as true. Also WingedPanther is correct about the lazy evaluation. Conditional statements always evaluate lazily when they can.
However, you made a mistake in evaluating the reverse of the equation.
&& has higher order than || so it is evaluated first, which means that ++i would never happen. In other wordsCode:m = ++i || ++j && ++k;
andCode:m = ++i || ++j && ++k;
are the same thing. Since the second part is true, because ++k and ++j are both non-zero, then the || operation receives true for one of it's inputs, and ignores the other one.Code:m = ++i || (++j && ++k);
-TheSourceOfX
I'm sorry, but I made a mistake. Your answer to the reversed equation was correct. The ++i does get evaluated. Lazy evaluation only works if the first input satisfies the operation.
After this code i=2 but j=1 because of lazy evaluation. Even though the second parameter in the first equation is "true" it goes from left to right, so ++i is still evaluated. The same goes for &&, just with false instead of true.Code:i = 1; j = 1; m = ++i || true; n = true || ++j;
Even though I made that mistake, however, you still need to keep in mind order of operations as I said before. && has a higher order than ||, so it happens first if there aren't any parentheses.
-TheSource
Actually the value of both is 1 since true=1 and false=0 and both of the OR operations take in at least 1 value of "true". However i=2 and j=1 after all 4 lines are executed. That is because of the lazy evaluation that ignores the ++j statement because the || operation already received one value of true, and that's all it needs to output true. You can think of || and && as functions such that
The code above illustrates exactly the type of lazy evaluation that is done for both the && and || operations. The only thing not represented in the code is that && is evaluated before || in order of operations just like multiplication is evaluated before addition.Code:bool || (bool a, bool b){ if(a == true){ return true; } if(b == true){ return true; } return false; } bool && (bool a, bool b){ if(a == false){ return false; } if(b == false){ return false; } return true; }
-TheSourceOfX
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks