Hi, pkpawan123 –
It’s definitely not a compiler bug.
I checked your code with a bunch of scenarios, and this line is always executing properly…
That is, if
N[i] is even at the time, it executes the “if” portion. Otherwise, it executes the “else” portion.
Of course, the value of
N[i] changes over time. For example if it starts at 5, then the
cout statements will output: 2 3 4 2 3 4 1.
N[i] is initially 5, which is odd, so the ‘else’ section executes (resulting in an output of 2 3 4), which eventually changes
N[i] to 3.
Then the j-based for loop repeats:
j is now 2, so the loop continues, and the ‘else’ section is executed again (so you see another 2 3 4), resulting in
N[i] being changed to 2.
Then the j-based for loop repeats again.
j is now 4, so the for loop ends, since
4 > N[i]. Then outer while loop executes again:
N[i] is still
!=1, so while loop continues.
Then the j-based for loop restarts (
j is back to 0), and the “if” statement is true (since 2
%2==0) – this results in
cout outputting the final 1. That “if” code also results in
N[i] being changed to 1.
Then, one final time, the j-based for loop repeats:
j is now 2, so the for loop ends. Then the outer while loop kicks in:
N[i] is now 1, so it ends.
So, bottom line, the code is working correctly from a compiler point of view. But perhaps it’s not working as you logically intend it to. Good luck!