Loop structure In Programming in C
Types of Loops in C
Depending upon the position of a control statement in a program, looping in C is classified into two types:
1. Entry controlled loop
2. Exit controlled loop
In an entry controlled loop, 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 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.” Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
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 for loop
2. The while loop
3. The do-while loop
(1). for loop.
Loops are used to repeat a block of code.
Syntax of for Loop :
for (init; condition; increment)
{
// block of statement.
}
Example :
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 10 ; i++)
{
printf("%d ",i);
}
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
Explanation :
init – Initializes the variable at the beginning of the loop to some value. This value is the starting point of the loop.
condition – Decides whether the loop will continue running or not. While this condition is true, the loop will continue running.
increment – The part of the loop that changes the value of the variable created in the variable declaration part of the loop. The increment statement is the part of the loop which will eventually stop the loop from running.
(2).While loop.
while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.
Syntax :
while( condition )
{
statement(s);
}
Example :
#include <stdio.h>
int main ()
{
// local variable definition
int a = 1;
// while loop execution
while( a < 5 )
{
//loops comes inside this body, until condition is true
printf("Value of a: %d\n", a);
a++;
}
return 0;
}
Output :
Value of a: 1
Value of a: 2
Value of a: 3
Value of a: 4
(3).do-while-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. The conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
Syntax :
do
{
statement(s);
} while( condition );
Example :
#include <stdio.h>
int main ()
{
// declared local operand (variable)
int a = 1;
// do-while loop
do
{
printf("value of a: %d\n", a);
a = a + 1;
} while( a < 5 );
return 0;
}
Output :
value of a: 1
value of a: 2
value of a: 3
value of a: 4
One more Example where condition is false :
#include <stdio.h>
int main ()
{
// declared local operand (variable)
int a = 1;
//here, Condition is false. a is not equals to zero
do
{
printf("value of a: %d\n", a);
a = a + 1;
} while( a == 0 );
return 0;
}
Output :
value of a: 1
I Hope It will helpful for you.