Is it illegal to modify a for loop counter inside the block?
No, the compiler allows it. However, it is considered bad practice because it makes the loop's behavior unpredictable and hard to debug.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in The Danger of Modifying Loop Counters Manually
If you increment the counter manually inside the loop, the loop's natural update step will increment it again, causing it to skip an element.
Use a while loop. It places the responsibility of updating the counter entirely inside the block, making dynamic jumps explicit and clear.
Yes. An unexpected double increment can push the counter past the bounds of an array, triggering an IndexOutOfBounds exception.
Still have questions?
Browse all our FAQs or reach out to our support team
