U
U
unit_9112020-10-27 17:25:20
C++ / C#
unit_911, 2020-10-27 17:25:20

Why doesn't prefix increment in boolean expression add one after evaluation?

int i, l, j, k;
    i = l = j = k = 0;
    int a = i++ && ++j || k || l++;

The output is expected to be: However, it is: Why did the postfix increment in the boolean expression increase by one, but the prefix increment did not? i = 1 l = 1 j = 1 k = 0 a = 0
i = 1 l = 1 j = 0 k = 0 a = 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-10-27
@unit_911

Because i++ == 0 which translates to false. So the second term of the conjunction (++j) makes no sense to calculate, the result does not depend on it. The first term of the disjunction is false, so we calculate the second one (k == 0). It is also false, so we evaluate the third one (l++) and take its value as the result of the expression.
In general, conjunction (&&) evaluates to the first false encountered, disjunction (||) evaluates to the first true encountered.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question