“Loop in C – Properties, and Applications”. We will cover Introduction, Details, Advantages, History, Types of Loops, Flowcharts, Practical Use, Outputs, and Conclusion. What is a loop? We are all familiar with the idea of listening to songs on loops. But, we are obviously not here to speak on that.
What is a loop in C? Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. It has mainly 2 parts – a body of a loop and a control statement. The main purpose is to repeat the same code a number of times.
3. Electrical Engineering Department
What is a Loop ?
Looping Statements in C execute the sequence
of statements many times until the stated
condition becomes false. A loop in C consists of
two parts, a body of a loop and a control
statement. 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.
4. Electrical Engineering Department
LOOP is nothing but a language that precisely captures primitive recursive
functions. The only operations supported in the language are assignment,
addition, and looping a number of times that is fixed before loop execution
starts.
The LOOP language was introduced in 1967 by Albert R. Meyer and Dennis M.
Ritchie. Meyer and Ritchie showed the correspondence between the LOOP
language and primitive recursive functions. Dennis M. Ritchie mainly
formulated the LOOP language.
It is known that the FOR loop has been part of the C language since the early
1970s (or late 1960s) and was developed by John Backus, but the DO loop
has been a part of Fortran since it was developed in the mid-1950s by John
Backus and his team at IBM.
The name for-loop comes from the word for, which is used as the keyword in
many programming languages to introduce a for-loop. The term in English
dates to ALGOL 58 and was popularized in the influential later ALGOL 60.
The loop body is executed "for" the given values of the loop variable, though
this is more explicit in the ALGOL version of the statement, in which a list of
possible values and/or increments can be specified.
(There’s no relevant name or origin related history for while & do while loop)
History of LOOPs
5. Electrical Engineering Department
Advantages of LOOPs
● It provides code reusability.
● Using loops, we do not need to write the same code
again and again.
● Using loops, we can traverse over the elements of
data structures (array or linked lists).
8. Electrical Engineering Department
In programming, LOOPs are considered as controlled
statements that can regulate the flow of the program
execution. They use a conditional expression in order to
decide to what extent a particular block should be
repeated. Every loop consists of two sections, loop body
and control statement.
Based on the position of these two sections, loop
execution can be handled in two ways that are at the
entry-level and exit-level.
So, loops can be categorized into two types:
● Entry controlled loop: When a condition is evaluated
at the beginning of the loop.
● Exit controlled loop: When a condition is evaluated at
the end of the loop.
9. Electrical Engineering Department
Entry Controlled Loop: An entry control loop checks
condition at entry level (at beginning), that’s why it is
termed as entry control loop. It is a type of loop in which
the condition is checked first and then after the loop
body executed. For loop and while loop fall in this
category. If the condition is true, the loop body would be
executed otherwise, the loop would be terminated.
Exit Control Loop: An exit control loop checks condition
at exit level (in the end), that’s why it is termed as exit
control loop. Oppose to Entry controlled loop, it is a loop
in which condition is checked after the execution of the
loop body. Do while loop is the example. The loop body
would be executed at least once, no matter if the test
condition is true or false.
10. Electrical Engineering Department
FOR LOOP
● A For loop is the most efficient loop structure in C
programming. It allows us to write a loop that needs
to execute a specific number of times. The for loop in
C language is used to iterate the statements or a part
of the program several times. It is frequently used to
traverse the data structures like the array and linked
list.
● Syntax of for loop in C
The syntax of for loop in c language is given below:
for( initialization; condition; increment/decrement)
{
//code to be executed
}
11. Electrical Engineering Department
How for loop works?
● The initialization statement is executed only once.
● Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is
terminated.
● However, if the test expression is evaluated to true,
statements inside the body of the for loop are
executed, and the update expression is updated.
● Again the test expression is evaluated.
*This process goes on until the test expression is false.
When the test expression is false, the loop terminates.
13. Electrical Engineering Department
A While Loop is used to repeat a specific block of code an
unknown number of times, until a condition is met. For
example, if we want to ask a user for a number between 1
and 10, we don't know how many times the user may enter
a larger number, so we keep asking "while the number is
not between 1 and 10". If we (or the computer) knows
exactly how many times to execute a section of code
(such as shuffling a deck of cards) we use a for loop.
The syntax of while loop in c language is given below:
while (condition){
//code to be executed
}
Flowchart and Example of while loop
While LOOP
14. Electrical Engineering Department
Why While Loops?
● Like all loops, "while loops" execute
blocks of code over and over again.
● The advantage to a while loop is that
it will go (repeat) as often as
necessary to accomplish its goal.
15. Electrical Engineering Department
How while Loop works?
In while loop, condition is evaluated first
and if it returns true then the statements
inside while loop execute, this happens
repeatedly until the condition returns
false. When condition returns false, the
control comes out of loop and jumps to the
next statement in the program after while
loop.
17. Electrical Engineering Department
The do...while statement creates a loop that executes a specified
statement until the test condition evaluates to false. The condition is
evaluated after executing the statement, resulting in the specified
statement executing at least once.
The syntax for do…while loop is stated below:
do
{
//code to be executed
}while(condition);
● Unlike for and while loops, that test the loop condition at the top of
the loop, the do...while loop checks its condition at the bottom of
the loop.
● A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time, where only while
loop executes the target statement, repeatedly.
Do while LOOP
18. Electrical Engineering Department
How Do...While loop works
The conditional expression appears at the end of the
loop, so the statement(s) in the loop executes once
before the condition is tested.
If the condition is true, the flow of control jumps back
up to do, and the statement(s) in the loop executes
again. This process repeats until the given condition
becomes false.
20. Electrical Engineering Department
DIFFERENCE BETWEEN WHILE LOOP AND DO WHILE LOOP
● Condition is checked first then
statement(s) is executed.
● It might occur statement(s) is
executed zero times, If condition
is false..
● No semicolon at the end of
while.
while(condition)
● If there is a single statement,
brackets are not required.
● Variable in condition is initialized
before the execution of loop.
● while loop is entry controlled
loop.
● while (condition)
{ statement(s); }
● Statement(s) is executed at
least once, thereafter condition
is checked.
● At least once the statement(s) is
executed.
● Semicolon at the end of while.
while(condition);
● Brackets are always required.
● variable may be initialized
before or within the loop.
● do-while loop is exit controlled
loop.
● do { statement(s); }
while(condition);
While loop Do-while loop
21. Electrical Engineering Department
NESTed LOOP
A nested loop has one loop inside of another. These are typically
used for working with two dimensions such as printing stars in
rows and columns the syntax are shown below. When a loop is
nested inside another loop, the inner loop runs many times inside
the outer loop. In each iteration of the outer loop, the inner loop
will be re-started. The inner loop must finish all of its iterations
before the outer loop can continue to its next iteration.
Syntax of Nested loop
OuterLoop
{
InnerLoop
{
// inner loop statements.
}
// outer loop statements.
}
23. Electrical Engineering Department
Infinitive
loop
To make a for loop infinite,
we need not give any
expression in the syntax.
Instead of that, we can put
two semicolons to validate
the syntax of the for loop. It
will work as an infinite for
loop.
For example :
#include <stdio.h>
void main()
{ int i = 10;
for( ; ;)
{
printf("%dn",i);
}
}
In while loop, when the
condition passes and if it is
true , it runs infinite number
of times.
For example :
#include <stdio.h>
void main()
{ int i = 10;
while(1)
{
printf("%dt",i);
i++;
}
}
The do-while loop will run
infinite times if we pass any
non-zero value as the
conditional expression.
For example :
#include <stdio.h>
void main()
{ int i = 10;
do
{
printf("%dt",i);
i++;
} while(i);
}
24. Electrical Engineering Department
BREAK AND CONTINUE STATEMENTS
● When a break statement is
encountered inside a loop, the loop
is immediately terminated and the
program control resumes at the next
statement following the loop.
For the for loop, continue statement
causes the conditional test and increment
portions of the loop to execute. For the
while and do...while loops, continue
statement causes the program control to
pass to the conditional tests.
true
25. Electrical Engineering Department
Practical Use
Real World Examples of Loop
● Software of the ATM machine is in a loop to process
transaction after transaction until you acknowledge that
you have no more to do.
● Software program in a mobile device allows user to unlock
the mobile with 5 password attempts. After that it resets
mobile device.
● You put your favorite song on a repeat mode. It is also a
loop.
● You want to run a particular analysis on each column of
your data set.
28. Electrical Engineering Department
CONCLUSION
Looping in C or in any programming language is one of
the key concepts. There are generally two types that are
entry controlled and exit controlled loop. The loops or
statement blocks execute a number of times until the
condition becomes false.
❖ Advantages
● Using loops, we do not need to write the same code
again and again.
● Using loops, we can traverse over the elements of
data structures (array or linked lists).
● It provides code reusability.