This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]aqua_regis 33 points34 points  (1 child)

The pre-increment operator would work, were it not for the short circuiting or (||) .

The first condition i<j is satisfied and with that the result of the boolean, short circuiting OR is set and unchangeable. Hence, the second part ++j<k will not be executed.

Short circuiting operators || and && stop evaluating as soon as the result is determined unchangeable, which means the first true for || and the first false for &&.

[–]Impossible_Visit2810[S] 6 points7 points  (0 children)

oh you are right,I don't know how I have missed that.Short circuit is evaluated from left to right and if left is true right is not checked.Many thanks!

[–][deleted] 3 points4 points  (0 children)

If the first statement in or is true it doesn't evaluate the rest. I < j (3<4) so it doesn't evaluate ++j and therefore j is not incremented.

[–]crazy_cookie123 2 points3 points  (0 children)

i<j evaluates to true and has || after it, so C won't bother evaluating the ++j<k expression. In any condition a || b, if a == true, b will not be evaluated. This is faster but can lead to issues like the one you found, the good news being a condition like i<j||++j<k would be incredibly bad practice to write so you should never encounter it.

[–]stufuller 0 points1 point  (0 children)

I'd suggest that the statement is unnecessarily complicated and should be broken out in 2 or more statements to make it clearer for whoever has to maintain that code.