This type of control structures specify a block of one or more statements that are repeatedly executed until a condition is satisfied. These are also called iteration control structures.
There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is placed:
-at the beginning of the loop (leading decision loop)a
-at the end of the loop (trailing decision loop)
-a counted numberof times (counted loop)
1. Leading Decision Loop:
In such loops the testing of the condition is at the beginning of the loop. Examples are DO-WHILE loops. The only way to terminate the loop is to render the DO-WHILE loop false.
2. Trailing Decision Loop:
In such structures the testing of condition is done at the end of loop except that the logic to keep on repeating is same as in case of other repitition structures. REPEAT...UNTIL is an example of trailing decision loop. In trailing loops the statems ase executed at least once before the condition is tested. These loops are sued in almost all kinds of computer program e.g. to print student records, to continue taking imput until a certain limit etc.
3. Counted Loop:
These are also known as DO or FOIR loops. In such kind of repitition structures the programmer knowns in advance the exact number of loop iterations. The loop execution is controlled by an important variable called the loop index. We can now conclude that selection control structures and repitition control structures find their use in almost any application. Ising them wisely and judiciously depends on the person who is going to use these loops. Proper implementation of loop structures can reduce program complexity and cost a lot.
Some problems require a sequence of tasks to be processed repeatedly, e.g.:
-apply same processing to a set of different data items
-processing until and target point is reached
A block of statements that are to be repeated
-A condition that will determine if the loop processing should stop
-The condition is evaluated once in each iteration
-A variable that will change each time the loop is processed
A variable that will change each time the loop is processed
-tested in the condition
The following image is a visual, an example for you to see what is meant by "the loop" or repitition in a simple flow chart like this.
No comments:
Post a Comment