-1

Are these two loops the same? For some reason the second loop is off-by-one and I cannot figure out why.

while ( !b && ++n < WORD_COUNT ) b = mWords[n];
n++;
while ( !b && n < WORD_COUNT ) {
    b = mWords[n];
    n++;
}
user3405291
  • 6,162
  • 5
  • 43
  • 115
  • duplicate: https://stackoverflow.com/questions/25705000/the-difference-between-n-and-n-at-the-end-of-a-while-loop-ansi-c – OrenIshShalom Aug 03 '20 at 06:17

1 Answers1

2

When the predicate b was not successful, then a logic short-circuit may apply in the first form and the final increment of n may be skipped.

In the second form, the increment happens before predicate b is evaluated, so n is off-by-one when the loop exits.

Ext3h
  • 5,184
  • 15
  • 40