In the third article of this series, we talked about the for and while loops. Both are like telling someone to run an errand for you.
A for loop helps you go through a collection of items, like a bag or a list. It gets each item out for you, one after another, and once it reaches the end of the list, it stops checking and ends the loop.
A while loop is like telling the worker to keep looking around the market until he finds the orange stall. He keeps looking while the answer is still "not yet," and he stops once he finds it. A while loop is useful when you don't know how many times the loop will need to run before a condition is met. A for loop, on the other hand, is commonly used when you want to go through a collection of items or repeat an action a specific number of times.
So, what do break and continue have to do with for and while loops?
Imagine you want the worker from our earlier example to go through a bag of items using a for loop. You're specifically looking for a ball,
Discussion
Start the conversation
Your voice can be the first to spark an engaging conversation.