- What is a loop in c?
- Advantages of using a loop in c.
- Types of loops
- What is the difference between while loop and do while loop?
This tutorial covers the concepts of looping statements in c with example. Advantages of using loops have been explained in this tutorial. This tutorial also covers the types of loop such as for loop in c programming example, while loop in c programming example and do- while loop in c programming
What is a loop in c?
When we write a program to solve any problem then sometime we face a situation when some statements in the code need to be executed more than one time or fixed number of times. In general when statements are executed in sequential order then at first, the first statement is executed then second , then third and so on.
C Programming languages provide us the facility of various looping statements in c. A loop statement provides us the facility to execute a statement or set of statements multiple times or a fixed number of times based on the limit and condition given by programmer in code. One major advantage of using looping statements in c is that there is no need to write the same statement or code of statements again and again.
In the next section of this c programming tutorial we will discuss different types of loop in c.
looping in cTypes of loop
There are following types of Looping statements in c programming are as follow
for loop in c programming
The for loop in c programming is a programming language conditional iterative statement which is used to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met.
The for loop is distinguished from other looping statements through an explicit loop counter or loop variable which allows the body of the loop to know the exact sequencing of each iteration.
The general syntax of for loop in c programming is as follows:-
for (initialization; condition section; increment /decrement section)
{
//block of statements ;
}
In the initialization section we have to initialize the variables with some value.
In the condition section – In this section we give a limit or condition which decides whether the loop will continue or not. While this condition is true the loop will continue running. In the increment/decrements section we have to either increment the value or decrements the value based on our requirement.
for loop in c programming example
To print number form 1 to 10 using for loop.
Void main()
{
for(int i=1;i<=10;i++)
{
printf(“n%d”,i);
}
getch();
}
while loop in c programming
While loop is a most basic loop in C programming. while loop has one control condition, and executes as long the condition is true. The condition of the loop is tested before the body of the loop is executed, hence it is called an entry-controlled loop.
The general syntax of while loop is as follows:-
while( condition)
{
//block of statements;
}
In while loop at first it will check the given condition, if the given condition is true then the statements are executed and these statements are executed till the given Condition is true when the given condition is false then the loop is automatically terminated.
while loop in c programming example
Program to print number form 1 to 10 using while loop.
Void main()
{
int i=1;
While(i<=10)
{
printf(“n%d”, i);
i++;
}
getch();
do while loop in c
In some cases it is required to execute the body of the loop before the condition expression is evaluated. In such situation we use the do while loop. When program control reaches at do statement then the body of lop is evaluated and at the end of loop condition expression is checked whether the condition is true or false. If the condition is true then the body of the loop is further evaluated and if the condition is false then control goes to the next statement immediately after the while statement.
The general syntax of a do-while loop is as follows:
do
{
;
} while(condition) ;
The execution of the above code is as follows:- Firstly the statements of the do loop will execute and then if will check the given condition and the statement are executed till the given condition is and when the Given condition is false then the loop is automatically terminated.
do while loop in c example
To program to print number from 1 to 10 using do while loop.
void main()
{
int i=1;
do
{
printf(“n%d”, i);
i++;
} while (i<=10);
getch();
}
Difference between while and do while loop
The difference between while and do while loop is that in while loop, firstly condition is checked and if the condition is true then statements are executed and the condition is false the loop is Automatically terminated, but in do while loop firstly the the statements are executed and then it will check the given condition i.e. the do while loop is executed at least once.
I hope that this tutorial will be helpful to students in understanding the looping statements in c programming, types of loop .