Def:- Executing a set of statements repeatedly for some number of times or
until some condition or event.
Ex:- print
numbers between 1 to 20 without loops.
void
main()
{
printf(“1”);
printf(“2”);
printf(“3”);
printf(“4”);
.
.
.
printf(“20”);
}
The same printf() statement has to be repeated 20 times to print the
numbers. To avoid this type of complexities, Loops were introduced.
Executing a set of
statements repeatedly some number of times or until some condition, is called
looping. In looping, sequences of statements are executed until some conditions
for termination of the loop are satisfied. A program loop therefore consists of
two Segments: Body of the loop and Control statements. The control statement
tests certain conditions and then directs the repeated execution of the
statements contained in the body of the loop.
The Test may be
either to determine whether the loop has been repeated the specified number of
times (Counter controlled loops) or to determine whether a particular condition
or event has been met (Event controlled loops).
In looping
process in general would include the following four steps
1. Setting and initialization of a counter i.e. loop control variable
2. Exertion of the statements in the loop
3. Test for a specified conditions for the execution of the loop
4. Incrementing the counter
C language provides 3 loop structures
1. While Loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
2. do-while Loop
It is like a while statement, except that it tests the condition at the end of the loop body.
3. for Loop
It is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
No comments:
Post a Comment