Loops are a programming construct that denote a block of one or more statements that are
repeatedly executed a specified number of mes, or ll a certain condi on is reached.
Repe ve tasks are common in programming, and loops are essen al to save me and
minimize errors. In C programming, the keywords while, do-while and for are provided to
implement loops.
Looping constructs are an important part of any processing logic, as they help in performing the
same process again and again.
Programming languages provide various control structures that allow for more complicated
execu on paths. A loop statement allows us to execute a statement or group of statements
mul ple mes.
Flowchart of C Loop Statement
Given below is the general flowchart of a loop statement which is applicable to any
programming language −
The statements in a C program are always executed in a top-to-bo om manner. If we ask the
compiler to go back to any of the earlier steps, it cons tutes a loop.
Example: Loops in C
To understand the need of loops in a program, consider the following code snippet −
#include <stdio.h>
int main (){
// local variable defini on
int a = 1;
prin ("a: %d\n", a);
a++;
prin ("a: %d\n", a);
a++;
prin ("a: %d\n", a);
a++;
prin ("a: %d\n", a);
a++;
prin ("a: %d\n", a);
return 0;
}
Output
On running this code, you will get the following output −
a: 1
a: 2
a: 3
a: 4
a: 5
The program prints the value of "a", and increments its value. These two steps are repeated a
number of mes. If you need to print the value of "a" from 1 to 100, it is not desirable to
manually repeat these steps in the code. Instead, we can ask the compiler to repeatedly execute
these two steps of prin ng and incremen ng ll it reaches 100.
Example: Using While Loop in C
You can use for, while or do-while constructs to repeat a loop. The following program shows
how you can print 100 values of "a" using the "while" loop in C −
#include <stdio.h>
int main () {
// local variable defini on
int a = 1;
while (a <= 100){
prin ("a: %d\n", a);
a++;
}
return 0;
}
Output
Run this code and check the output −
a: 1
a: 2
a: 3
a: 4
.....
.....
a: 98
a: 99
a: 100
Parts of C Loops
To cons tute a loop, the following elements are necessary −
Looping statement (while, dowhile or for)
Looping block
Looping condi on
Loops are generally of two types −
Counted Loops in C
If the loop is designed to repeat for a certain number of mes, it is a counted loop. In C,
the for loop is an example of counted loop.
Condi onal Loops in C
If the loop is designed to repeat ll a condi on is true, it is a condi onal loop. The while and do-
while constructs help you to form condi onal loops.
Looping Statements in C
C programming provides the following types of loops to handle looping requirements −
[Link]. Loop Type & Descrip on
1 while loop
Repeats a statement or group of statements while a given condi on is true. It tests the
condi on before execu ng the loop body.
2 for loop
Executes a sequence of statements mul ple mes and abbreviates the code that
manages the loop variable.
3 do-while loop
It is more like a while statement, except that it tests the condi on at the end of the loop
body.
4 nested loops
You can use one or more loops inside any other while, for or do-while loop.
Each of the above loop types have to be employed depending upon which one is right for the
given situa on.
Loop Control Statements in C
Loop control statements change the execu on from its normal sequence. When execu on
leaves a scope, all automa c objects that were created in that scope are destroyed.
C supports the following control statements −
[Link]. Control Statement & Descrip on
1 break statement
Terminates the loop or switch statement and transfers execu on to the statement
immediately following the loop or switch.
2 con nue statement
Causes the loop to skip the remainder of its body and immediately retest its condi on
prior to reitera ng.
3 goto statement
Transfers the control to the labeled statement.
The break and con nue statements have contras ng purposes. The goto statement acts as a
jump statement if it causes the program to go to a later statement. If the goto statement
redirects the program to an earlier statement, then it forms a loop.
The Infinite Loop in C
A loop becomes an infinite loop if a condi on never becomes false. An infinite loop is a loop
that repeats indefinitely because it has no termina ng condi on, or the termina on condi on is
never met or the loop is instructed to start over from the beginning.
Although it is possible for a programmer to inten onally use an infinite loop, they are o en
mistakes made by new programmers.
Example: Infinite Loop in C
The for loop is tradi onally used for crea ng an infinite loop. Since none of the three
expressions that form the "for" loop are required, you can make an endless loop by leaving the
condi onal expression empty.
#include <stdio.h>
int main (){
for( ; ; ){
prin ("This loop will run forever. \n");
}
return 0;
}
Output
By running this code, you will get an endless loop that will keep prin ng the same line forever.
This loop will run forever.
This loop will run forever.
........
........
This loop will run forever.
When the condi onal expression is absent, it is assumed to be true. You may have an
ini aliza on and increment expression, but C programmers more commonly use
the for(;;) construct to signify an infinite loop.
Note − You can terminate an infinite loop by pressing the "Ctrl + C" keys.