When you want to perform an action over and over, like processing a record or doing some computations.
What type? It’s all based on a condition being met. It may be the length of a sentence, and doing something with each character. Or it may be counting down a number to zero.
When that condition is met, then the loop terminates and processing continues.
May sound a bit unusual, but based on the question you’re asking. And in a comment you said you’re just trying to learn. I can reccomend a few games you can play to get you familiar with coding and how to structure programs. One I would reccomend is ‘Human Resource Machine’. This game focuses heavily on loops and can somewhat give you an idea of how functions come into play.
A for loop is used when you want to do something a specific number of times. They’re often used if you need to perform some operation on every member of an array. A for each loop is pretty similar to a for loop, just with slightly different a syntax.
A while loop is used when you need the loop to keep executing the loop and unknown number of times until some condition is met. A do while loop is similar to a while loop but is guaranteed to execute at least once.
Latest Answers