Structures - Loops

LoopsA repetition structure, also known as a loop, causes a statement or set of statements to execute repeatedly. Any time we need a program to perform the same task over and over, we need a repetition structure (a loop).

Long sequence structures could be used instead of loops, but it is much more efficient to use a repetition structure. This reduces duplicated code, saves time, and makes it much easier to modify the code in the future.

Loops can be set up to repeat until a condition is true (Do-Until loop). A loop can also be set to repeat while a condition is true (While loop and Do-While loop). We can test before (pretest), or after (posttest), to see when the condition is true. Other loops can repeat a specific number of times (For loop) when we know in advance how many times we want the loop to repeat.

Let's take a look at different types of loops.