Home »
C programming language
C Language Control Statements
C Loopings - for, while, do while
Looping (iteration) is the method (technique) which repeats the set of statement written in the loop body continuously until the certain condition (test-expression) is TRUE.
Basically there are two types of loops in C Language sentinel control loop & counter control Loop. In Counter Controlled loop, we know that exactly how many times loop body will be executed while in Sentinel Controlled loop we don’t know about the loop recurrence, Execution of loop is based on condition not counter.
There are three types of Looping provided in C Language:
- The while loop
- The for loop
- The do while loop
The while loop
while is an entry controlled loop, test-condition is checked before executing the body of the loop ( set of statements) . while loop executes the given set of statements until the given test condition if false.
Syntax
initialization of loop counter;
while(test-condition)
{
Set of statements/ loop body;
update loop counter;
}
Consider the example:
/* program to print numbers from 1 to 10 using while loop*/
#include < stdio.h >
int main()
{
int cnt; // loop counter
cnt=1; // initialization of loop counter
while(cnt <= 10)
{
printf("%d\n",cnt);
cnt++; // update loop counter
}
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
The for loop
for is an entry controlled loop, test-condition is checked before executing the body of the loop ( set of statements) .for loop executes the given set of statements until the given test condition if false. for loop contains the initialization, test-condition and update loop counter with the for keyword in a bracket separated by semicolon ";".
Syntax
for(initialization of loop counter; test-condition; update loop counter;)
{
Set of statements/ loop body;
}
Consider the example:
/* program to print numbers from 1 to 10 using for loop*/
#include < stdio.h >
int main()
{
int cnt; // loop counter
// initialization of loop counter, body and, // update loop counter
for(cnt=1; cnt <= 10; cnt++)
{
printf("%d\n",cnt);
}
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
The do while loop
do while is an exit controlled loop, body of the loop is executed first after that test-condition is checked, do while also executes the loop body until the condition is false. do while executes loop body at least once even test-condition is false in starting.
Syntax
initialization of loop counter;
do
{
Set of statements/ loop body;
update loop counter;
}while(test-condition);
Consider the example:
/* program to print numbers from 1 to 10 using do while loop*/
#include < stdio.h >
int main()
{
int cnt; // loop counter
cnt=1; // initialization of loop counter
do
{
printf("%d\n",cnt);
cnt++; // update loop counter
}while(cnt <= 10);
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
Infinite loop
The loop which never ends is known as infinite loop. This type of loop is very useful in project (real life application) where you do not want to quit from the loop after the certain repeat. Embedded System application is the most useful example of infinite loop. Infinite loop can be made using any one of the loop from while, for or do while. Just put the non zero value instead of test-condition.
Syntax
//using while loop
while( non-zero-value)
{
Set of statements/ loop body;
}
//using for loop
for( ; non-zero-value; )
{
Set of statements/ loop body;
}
//using do while loop
do
{
Set of statements/ loop body;
} while( non-zero-value);
Consider the example:
/* program to print your name infinite */
#include < stdio.h >
int main()
{
char name[]="Mr. XYZ";
while(1)
{
printf("%s \t",name);
}
return 0;
}
Output
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ Mr. XYZ
Mr. XYZ .....
When do while should be used?
do while should be used, when you want to execute some set of statements, then you want to check test-condition.
For example, if you want to check entered number is positive, negative or zero then ask for the user to do it again.
Consider the example:
/* program to check enetered number is ZERO, POSITIVE or NEGATIVE until
user do not want to quit
*/
#include < stdio.h >
int main()
{
int num;
char choice;
do
{
printf("Enter an integer number :");
scanf("%d",&num);
if(num==0)
printf("Number is ZERO.");
else if(num>0)
printf("Number is POSITIVE.");
else
printf("Number is NEGATIVE.");
printf("\n\nWant to check again (press Y/y for 'yes') :");
fflush(stdin); /*to clear input buffer*/
scanf("%c",&choice);
}while(choice=='Y' || choice=='y');
printf("\nBye Bye!!!");
return 0;
}
Output
Enter an integer number :0
Number is ZERO.
Want to check again (press Y/y for 'yes') :Y
Enter an integer number :1234
Number is POSITIVE.
Want to check again (press Y/y for 'yes') :Y
Enter an integer number :-345
Number is NEGATIVE.
Want to check again (press Y/y for 'yes') :y
Enter an integer number :45
Number is POSITIVE.
Want to check again (press Y/y for 'yes') :N
Bye Bye!!!
Nested looping
Loop within loop or Nested looping can be achieved by using loop statement with in the loop statement. You can use any type of loop with in the looping statements, it is not necessary that use same loop as inner loop.
Consider the example:
/*program to print tables from 1 to 20*/
#include < stdio.h >
int main()
{
int i,j; /*Here, we will use i for outer loop counter
and j for inner loop counter*/
int num;
for(i=1; i<=20; i++) /*to print table 1 to 20*/
{
/*each number has 10 multiples*/
num= i; /*to initialize number with i ( 1 to 20)*/
for(j=1; j<=10; j++)
{
printf("%3d\t",(num*j)); /*values will be padded with 3 spaces*/
}
printf("\n"); /*after printing table of each number*/
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
13 26 39 52 65 78 91 104 117 130
14 28 42 56 70 84 98 112 126 140
15 30 45 60 75 90 105 120 135 150
16 32 48 64 80 96 112 128 144 160
17 34 51 68 85 102 119 136 153 170
18 36 54 72 90 108 126 144 162 180
19 38 57 76 95 114 133 152 171 190
20 40 60 80 100 120 140 160 180 200