Looping
Looping
What is Loop in C?
• Looping Statements in C execute the sequence of statements many times until the stated condition
becomes false.
• The control statement is a combination of some conditions that direct the body of the loop to
execute until the specified condition becomes false.
• The purpose of the C loop is to repeat the same code a number of times.
Types of Loops in C
• Depending upon the position of a control statement in a program, looping statement in C is classified
into two types:
1. Entry controlled loop
2. Exit controlled loop
• In an entry control loop in C, a condition is checked before executing the body of a loop. It is also
called as a pre-checking loop.
• In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called
as a post-checking loop.
• The control conditions must be well defined and specified otherwise the loop will execute an infinite
number of times.
• The loop that does not stop executing and processes the statements number of times is called as
an infinite loop. An infinite loop is also called as an “Endless loop.”
The specified condition determines whether to execute the loop body or not.
'C’ programming language provides us with three types of loop constructs:
1. The while loop
2. The do-while loop
3. The for loop
1. while loop in C
• If condition is true, statements inside the body of while loop are executed. Then, condition is
evaluated again.
Looping Page 1
Do-While loop in C
• A do…while loop in C is similar to the while loop except that the condition is always executed after the
body of a loop.
• In this loop, the statements the loop need to be executed at least once.
• After that, it checks the condition. If the condition is true, it will again have executed the loop; otherwise,
it will exit it.
• The while loop is performed only when the condition is true, but sometimes the statement must be
conducted at least once, so the do-while loop has to be used.
• The difference between while and do-while loop is that in the while loop, while is written in the
beginning, and do-while, the condition is mentioned at the end and ends with a semicolon (;).
Looping Page 2
For loop in C
• For loop is an entry controlled loop i.e. the condition is checked before entering into the loop.
• So if the condition is false for the first time, the statements inside while loop may not be executed at all.
• In order to exit from a for loop, either the condition should be false or a break statement should be encountered.
• For loop is suitable to use when we have to run a loop for a fixed number of times.
Looping Page 3
Nested loop in C
• A loop inside another loop is called a nested loop.
• Consider a nested loop where the outer loop runs n times and consists of another loop inside it.
• The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.
Looping Page 4
Note: There can be mixed type of nested loop i.e. a for loop inside a while loop, or a while loop inside a do-while loop.
Looping Page 5
Looping Page 6
Looping Page 7
Looping Page 8
Looping Page 9