ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Introduction to Computer Programming• 6 min read

Loops & Iteration: For, While & Foreach

Automate repetitive operations with for loops and while loops, and control iteration flow with break and continue.

For Loops vs. While Loops

For Loop: Best used when you know in advance how many iterations are needed, or when looping over a known collection of items.
While Loop: Repeats as long as a dynamic boolean condition remains true. Essential when the number of cycles depends on external events (e.g. waiting for network response or user input).
break: Instantly aborts and exits the loop.
continue: Skips the remainder of the current iteration and jumps directly to the next cycle.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which keyword immediately terminates the enclosing loop prematurely?