5 Looping
5 Looping
Control Statements:Looping–Repetition
- Iteration and repetitive execution (for, while, do-while), nested loops.
Loop-loop is a control structure that repeats a group of any kind of statements in a program.
Loop body- The statements that are repeated in the loop.
Looping Constructs in C:
A set of statements may have to be executed number of times or till the condition is satisfied are
called Looping Constructs.
The statements that help us to execute a set of statements repeatedly are called Looping Constructs.
It is implemented using the following statements.
for
while
do-while
The loop will have to perform 3 tasks
Initialization : Loop Initialization.
Condition checking : Loop Condition.
Incrementation / decrementation : Loop Updating.
The difference between 3 looping control is the order in which they are represented.
Three questions to determine whether loops will be required in the general algorithm:
1. Are there any steps repeated while solving the problem? If so, which ones?
2. Do we know in advance how many times these steps are repeated?
3. Do we know the condition until when we keep repeating these steps?
Explanation:
In case of for loop, the Initialization, Condition check and Incrementation is represented in the same line.
First Initialization of the loop is performed.
Next condition is checked. If condition is true then the control enters the body of the loop and
statements are executed.
Next Incrementation of the loop is performed after which again the control goes back to the
condition check, if condition is true, the body is executed again.
This process repeats as long as the condition evaluates to be true. Once the condition evaluates to be
false, the control flows to the statement outside the body of the loop.
Statement1;
Statement2;
initialization;
while(Condition check)
{
Statement3;
Statement4;
incrementation;
}
Statement5;
Explanation:
In case of while loop, the Initialization, Condition check and Incrementation is represented in the separate
statements.
First Initialization of the loop is performed.
Next condition is checked. If condition is true then the control enters the body of the loop and
statements are executed.
Next Incrementation of the loop is performed, which is represented in loop body after which again
the control goes back to the condition check, if condition is true, the body is executed again.
Syntax: Flowchart:
Statement1;
Statement2;
initialization;
do
{
Statement3;
Statement4;
incrementation;
}
while(condition check);
Statement5;
Explanation:
In case of for do-while, the Initialization, Condition check and Incrementation is done in the separate
statement.
First Initialization of the loop is performed.
Next condition is not checked. Rather body of the loop is entered and statements are executed.
“What are the Entry controlled and Event/ Exit controlled loops in C?”
Nested Loops
Using one loop inside another loop is called Nested Looping. You can use one or more loops inside
any other while, for, or do-while loop.
Syntax:
The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
}
}
}
Infinite loops
A loop becomes an infinite loop if a condition never becomes false.
The for loop is traditionally used for this purpose. Since none of the three expressions that form the
‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty.
Syntax:
for( ; ; )
{
Statement to execute for ever;
}
Example:
When the conditional expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but C programmers more commonly use the for(;;) construct to signify an
infinite loop.
It can also be implemented with while and do-while loops also.