0

Why does this:

while (arr[i] == arr[i += shift]) { // not working
  if (i == target)
    return arr[i];
}

or this:

while (arr[i] == arr[i = (i + shift)]) { // not working
  if (i == target)
    return arr[i];
}

behave differently from this:

while (arr[i] == arr[i + shift]) {  // works
  i += shift;
  if (i == target)
    return arr[i];
}

My instinct is that ALL three should behave exactly the same.

Roman
  • 13
  • 3
  • 2
    Your instinct is therefore *wrong!* It's unwise to use side-effects (like `++`) until you understand their foibles and potentially undefined behaviour. I suggest you restrict yourself to readable code for now. – paxdiablo May 02 '17 at 03:47
  • The question you marked as duplicate claims these should have not unexpected behavior. "For example in i = i + 1 all the access of i (in L.H.S and in R.H.S) are directly involved in computation of the value to be written. So it is fine." – Roman May 02 '17 at 04:07
  • @Roman Your case is similar to Example 2 near the end of the accepted answer to the duplicate – M.M May 02 '17 at 04:20
  • @M.M I see now, you are exactly correct. Thanks all. – Roman May 02 '17 at 04:35

0 Answers0