Loops in C | What is Loops in C Programming

In this topic we are going to learn about different types of loops available in C language.

What is a loop?

A loop is a statement that allows you to execute a block of code a specified number of times or as many times as specified by the user until the statement turns false. 

The different types of loops provided in C are:

  • For loop
  • While loop
  • Do-while loop

Lets discuss them one by one.

loop in c programming
Loops in C Programming

For Loop:

In for loop, the initialisation statement is only executed once at the starting of the loop, then the test expression is tested.

If the test expression is satisfied then the body of the loop is printed and the expression is updated as instantiated by the update statement.

If the test expression results false the loop is terminated. The for loop is initialised with the for a keyword.

Syntax Of For Loop:

for condition
For Loop Condition
for(initialisation expression; test expression; update statement)
{
	//body of the loop
}

For Example:

//For example:
//Example to print 1 to 10 using for loop.

#include<stdio.h>
int main()
{
	int i;
	for(i=1;i<=10;i++)
	{
		printf("\n%d",i);
	}
	return 0;
}

In the above example the variable i of integer type is initialised with 1 and the test expression shows if i is less than equals to 10 and the update expression with i++, which states that when i is less than 10 till equals to 10 the value of i will be incremented by 1.

Output:

l7P6iqBMG6IrLwH 8MsKumCaRUXylEmruBeZm5Sal5OVyMl gbkb8Sx98wELNKTLAATQ IDA7fur qLdfyW81tmuFROs9hJE12jbwNkLbloGbiriHbWJFm50jNkbNr3hbjgkg

While Loop in C Programming:

In a while loop, the test expression is evaluated and tested.

If the condition is satisfied to true the body of the loop gets executed where the update statement is initialised in the body of the loop, until the test expression satisfies to false. In the case of the false, the loop gets terminated and the command gets out of the loop.

The while loop is initialized with the while keyword.

Syntax Of While Loop:

while( test expression)
{
	//body of the loop
}

For Example Of While Loop:

//For example:
//To print numbers from 1 to 10 using while loop

#include<stdio.h>
int main()
{
	int i=1;
	while(i<=10)
	{
		printf("\n%d",i);
		i++;
	}
	return 0;
}

In the above example the test expression inside the while loop shows if i is less than or equals to 10. The loop continues with the update expression where the value of i is incremented by 1 in every stage. We can initialise the value of i, if not initialised the value is automatically initialised with 0. If the loop tends to be false then the loop is terminated.

Output Of While Loop:

8 OgLvk lAgushWZEUdxcRVWUY89SZdw96QzydsskcMTRQS7Yka3KjRe IfVc nMe4QHcKFbwy 1niWrcgTgt5G3sKQUi mOP6P6SfSaOhkYQr2LB

do-while Loop In C Programming:

In the do-while loop, the body of the loop is executed once and then the test expression is evaluated. The loop gets terminated if the test expression results in false. The do-while loop is initialised with the do and while keyword.

Syntax For do-while Loop:

do
{
	//body of the loop
}
while(test expression); 

while(test expression); 

So while expression is a condition where the control of loops move towards beginning and if condition false then terminate.

Example Of do-while:

//For example:
//To print 1 to 10 using do while loop

#include<stdio.h>
int main()
{
	int i=1;
	do
	{
		printf("\n%d",i);
		i++;
	}
	while(i<=10);
	return 0;
}

In the above expression, the loop at first is executed and then the expression is checked and then with every states of the loop, the value of i is incremented with 1. When the test expression results to false then the loop gets terminated.

Output of do-while:

9G ghlYMU6D7Ia XsxC0u 470N1fYoTHdalmUVUjd23A89wNEFA8 50Hc9T2nTgUqW0MtZsgFiG uu9fuKPRvpB7jceTKKonzMe7NtUSwQH9zAfOqsESeBHSFcEr1tkXQAMsPg

Difference between while and do-while loop:

the most common question you get is the difference between while loop and do-while loop so let’s discuss it.

WhileDo while
Condition checked then executed Executed at least once, then condition is checked 
No semicolon in the end of while loopSemicolon in the end of while loop
No statement is executed if condition is falseStatement is executed once if condition is false
While loop is entry controlled Do while loop is exit controlled 
Difference between while and do-while loop

Here we come to the end of the discussion on loops in C, hope we have cleared all your dobts.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 36

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply