0

I have 3 variables, need to roll in the loop while x < y and y < z and increment y.

I saw this piece of code and I don't understand what the statement inside the brackets evauluates to:

while (x < y < z){
    y++;
}

Of course the right way to do it is:

while (x < y && y < z){
    y++;
}

For int x = 1, y = 2, z = 3 the first loop is infinite, and the second run one time, as expected.

My question is about the first condition statement - what actually happening?

Matan
  • 39
  • 4
  • 3
    `x < y` part returns 0 or 1 and then this result is compared with `z` – Iłya Bursov Sep 16 '22 at 14:55
  • 1
    `x < y` evaluates to `true`, `true` evaluates to `1`, `1 < z` evaluates to `true` – ks1322 Sep 16 '22 at 14:58
  • @IłyaBursov so in fact `(x < y < z)` is the same as `((x < y) < z)`? and for more variables, will `(x < y < z < w)` be the same as `(((x < y) < z) < w)`? – Matan Sep 16 '22 at 15:00
  • 4
    @Matan yes, it is left-to-right operator https://en.cppreference.com/w/cpp/language/operator_precedence – Iłya Bursov Sep 16 '22 at 15:01

0 Answers0