0

I=j=k=1; ++i || ++j && ++k why does it increment i only, and not j and k

&& has higher precedence, shud be evaluated before ||. So j and k shud be incremented before I.

10th Nov 2019, 11:36 AM
Sandeep Sood
Sandeep Sood - avatar
7 Respuestas
+ 1
Ah. Well. When you have `5 - 3*2`, the first thing that is being solved is `5`, if that makes sense. And once we have solved `5` (there is nothing to do), we then move on to whatever is right of the `-`. That's where precedence kicks in: `*` has higher precedence than `-`, so we need to solve all of `3*2` and that's what goes right of the `-`.
10th Nov 2019, 11:00 PM
Schindlabua
Schindlabua - avatar
+ 1
Ok Sir, Thanx for ur reply. But does it mean that in any expression first of all first operand (left most, after =) is solved first.
12th Nov 2019, 1:52 AM
Sandeep Sood
Sandeep Sood - avatar
0
You are right, && has higher precedence, so if we add parentheses the expression becomes ++i || (++j && ++k) However, precedence rules go no further than that. Evaluation order is still left-to-right. Additionally, `||` is "left-associative" which is why the left side of an `||` will be evaluated first, always. And of course as you probably know, `||` short-circuits and doesn't evaluate the right side because the left side is already `true`.
10th Nov 2019, 1:29 PM
Schindlabua
Schindlabua - avatar
0
Thanx for ur reply. As u have written "Evaluation order is still left-to-right" Then in 5-3*2 why 3*2 is solved first? Why in 2==3<4 3<4 is solved before == Kindly explain. Thanx and regards.
10th Nov 2019, 3:49 PM
Sandeep Sood
Sandeep Sood - avatar
0
[1/2] It's always more complicated.. First of all, there are right-associative operators, like `=`, where the right side will be evaluated first. (So you can do things like `a = b = c`) But apart from that, evaluation order is left-to-right. ...in most programming languages. C++ is unique, here we have very complicated "sequencing rules" as they are called. `||` is actually special in that the left side is guaranteed to happen first, always. (So we may short-circuit and skip the right side) However, in C++, if we take `a() + b() + c()`, the compiler can choose in which order to call the functions! The idea is that the compiler can do some more optimizations on your code if it can choose. (You can read the details at https://en.cppreference.com/w/cpp/language/eval_order ) So what *do* we know? Take 29 == 3 + 4 * 5 + 6 Due to operator precedence, this is the same as writing 29 == (3 + (4 * 5) + 6) And due to left-associativity, this is the same as writing 29 == ((3 + (4 * 5)) + 6)
12th Nov 2019, 10:40 PM
Schindlabua
Schindlabua - avatar
0
[2/2] But in C++, precedence and associativity do not tell you in which order the operands will be solved/evaluated. In 29 == 3 + 4 * 5 + 6 the `6` might be evaluated first or last. But we are guaranteed that the result must be equal to 29 == ((3 + (4 * 5)) + 6) because of what I wrote above. I hope that made some sense. Most of the time, for the programmer, this doesn't matter at all. But if we replace our above example with a() == b() + c() * d() + e() and any of these functions have side effects (database connections, changing global variables, ...), then this piece of code may not do what you expect, or maybe, when you run your program twice, you get two different results. But we know in which order the function results will be "summed up", because of precedence/associativity! C does it even differently, it has "sequence point rules" and the ideas are similar. C++ used the same rules until C++11. In all other programming languages, evaluation order is simply left to right.
12th Nov 2019, 10:48 PM
Schindlabua
Schindlabua - avatar
0
Ok sir. Thanx a lot.
13th Nov 2019, 1:29 AM
Sandeep Sood
Sandeep Sood - avatar