Looping Statements in C | while, do-while and for Loop
In C language, looping statements allow you to perform repeatition of a particular block of code.. In the process of looping, basically 4 steps are performed:
- Initialization of control variable.
- Execution of statements in the loop.
- Updation of control variable.
- Condition Testing.
Looping statements can be easily divided into 2 categories:
- Entry Control Loop: Entry Control loop tests the condition before execution. If the condition is false then the loop gets terminated.
- Exit Control Loop: In this looping, condition is tested after the execution of statements. It means that , first time, execution of statement is done unconditionally.
Let us see all the three looping statements in detail with example.
1. The while statement : The while statement is an entry controlled looping statement which tests the condition after the initialization of control variable. If the condition is true then. statements in curly braces of while statement are executed and if condition is false then loop gets terminated.
The general form of while statement is given below :
initialization; /*Control variable is initialized */
while(condition) /*Condition is tested */
{
Execution of statements ; /* Statements are executed if condition is true */
updation ; /*Control variable is Updated*/
}
statement x ; /* This is the default statement after loop */
The flow chart of while statement depicts the working flow of loop which is given below :
Let us understand the working of while loop with a program. In the program we will print even numbers from 0-100 with the help of while statement.
/* A program to print even numbers from 0-100 using while loop */
#include<stdio.h>
int main()
{
int x;
x=0;
while(x<=100)
{
printf("\t %d",x);
x=x+2;
}
return 0;
}
After executing above program you will get following output :
2. The do-while statement : The do-while statement is an exit control looping statement. In this statement condition is tested after the execution of statement. Loop continues till the condition is true and as the condition is false, loop gets terminated.
The general form of do-while statement is given below :
initialization; /*Control variable is initialized */
do
{
Execution of statements ; /* Statements are executed if condition is true */
updation ; /*Control variable is Updated*/
}while(condition) /*Condition is tested */
statement x ; /* This is the default statement after loop */
The flow chart of do-while statement depicts the working flow of loop which is given below :
Note: The first execution of statement inside do-while block is done unconditionally, that's why it is executed at least one time.
Let
us understand the working of do-while loop with a program. In the program
we will print the table of a Number input by user with the help of do-while statement.
/* A program to print the table of a Number input by user using dwhile loop */
#include<stdio.h>
int main()
{
int t,no,a;
printf("\n Enter a number to get its table\t");
scanf("%d",&no);
printf("\n The Table of %d is \n",no);
a=1;
do
{
t=no*a;
printf("\n %d * %d = %d",no,a,t);
a++;
}
while(a<=10);
return 0;
}
After executing above program you will get following output :
3.
The for statement : The for statement is an entry control
looping statement. It is a concise looping statement in which initialization, condition and updation are performed in a single line.
The general form of for statement is given below :
for ( initialization; condition; updation)
{
Execution of statement(s);
}
The flow chart of for statement depicts the working flow of loop which is given below :
The for loop continues till the condition is true and as the condition is false the loop gets terminated.
Let
us understand the working of for loop with a program. In the program
we will print a list of numbers from 1 to 50 with the help of for statement.
/* A program to print a list of numbers from 1 to 50 using for loop */
#include<stdio.h>
int main()
{
int x;
for (x=1;x<=50;x++)
{
printf("\t %d",x);
}
return 0;
}
After executing above program you will get following output :
0 Comments