2

Currently skim through about c++, but at below example my brain stuck. at the res variable, example have preincrement of a,b and c integer values.

When I print it out first one have output of a = 1, b = 0, c=0

at the second part output is a = 1, b = 0, c=0

    int a, b, c;
    a = 0;
    b = 0;
    c = 0;
    res = ++a || ++b && ++c;
    cout << '\n'
        << " res = " << res
        << ", a = " << a
        << ", b = " << b
        << ", c = " << c << endl;
    a = b = c = 0;
    res = ++a && ++b || ++c;
    cout << " res = " << res
        << ", a = " << a
        << ", b = " << b
        << ", c = " << c << endl;

both of them I'm expecting output as a = 1, b = 1, c=1 because I have already incremented variables. From output I understand if after || operator, nothing is evaluated? I need clarification. Thanks.

Omrum Cetin
  • 1,320
  • 13
  • 17
  • 1
    Lookup [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation). Evaluation of subexpressions stops as soon as the end result is unambiguously determined. – dxiv Sep 20 '20 at 02:28
  • 1
    @dxiv very good page and comment. Thanks – Omrum Cetin Sep 20 '20 at 02:43

0 Answers0