loop in c programming
C Programming c programming notes for gate C programming Tutorials gate study material for cse

Looping Statements in C Programming

Looping Statements in C Programming

Looping statements in C are used to repeat a block of code or statements until a condition is satisfied. In C Programming there are three types of Loops

  • For Loop
  • While Loop
  • 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.

Frequently asked Questions

Some frequently asked questions from loops in C programming are-

  • What is a looping statements  in c?
  • What are different types of Loops in C ?
  • what are 3 types of loops ?
  • Write advantages of using a loop in C Programming.
  • What is the difference between while loop and do while loop?

What is a Loop in C ?

A Loop in C Programming is used to repeat a block of code or statements until a condition is satisfied

When we write a program to solve any problem then sometime we face a situation where 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 looping statements in c 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 statements in c programming
looping in c

Types 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 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.

For Loop Syntax

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 step we have to initialize the loop variables with some value.

In the condition step – 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 step we have to either increment the value or decrements the value of loop variable based on our requirement.

For Loop Example

Example : Program in C to print number form 1 to 10 using for loop is given below

#include<stdio.h>

#include<conio.h>

Void main()
{
for( int i=1;i<=10;i++)
{
printf(“n%d”,i);
}
getch();
}

In this program i is the loop variable which it initialized with 1 to print the number from 1 to 10 and in condition testing step we have mentioned i<=10 it means loop will execute until the value of i become 10 and after each  iteration value of i is incrementing by 1. As sson as the value of i becomes 11 loop will stop and printf() statement written inside loop body will not be execute.

This program will print the number form 1 to 10.

While Loop in C Programming

  • While loop is  also known as entry controlled loop in c programming.
  • The condition of the loop is tested before the body of the loop is executed, hence it is called an entry-controlled loop.
  • While loop has one control condition, and executes as long the condition is true.

Syntax of While 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 Example

Program to print number form 1 to 10 using while loop.

#include<stdio.h>

#include<conio.h>

Void main()
{
int i=1;
while(i<=10)
{
printf(“n%d”, i);
i++;
}
getch();

clrscr();

}

In the above program i is the loop control variable which is initialized by 1 before starting of while loop. Condition (i<=10) will be  tested first if this condition is true the printf() statement written inside while loop will execute and value of i will be printed. After that value of i will be increase by 1 .

Do-While Loop 

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
{
//statement ;
} 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 Example

To program to print number from 1 to 10 using do while loop.

#include<stdio.h>

#include<conio.h>

void main()
{
int i=1;
do
{
printf(“n%d”, i);
i++;
} while (i<=10);
getch();

clrscr();

}

Difference Between While loop 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.

While loop is also known as entry controlled loop where as do while loop is also known as exit controlled loop.

Conclusion and Summary

  • In this tutorial we have discussed bout 3 types loops in c programming with example and programs.
  • We have also discussed the difference between different loops in c programming.
  • Syntax and programs foe each type of loop is explained.

hope that this tutorial will be helpful to students in understanding the looping statements in c programming, types of loop .

Leave a Reply

Your email address will not be published. Required fields are marked *