I n programming how does on know where to put loops and which one?



I n programming how does on know where to put loops and which one?

In: Other

What are you trying to accomplish?

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.

I usually think of what I want to accomplish ( like run through columns 1 to 10) and the end goal. Loops for me got a lot easier to understand with time

Generally, “for” loops are used when there is a definite number of times you need to do something, and “while” loops are used when the number of loops needed is unknown.

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.