Loops in c

If you are searching for loops in c then you are at the right blog post.

Loops in c: Programming languages provide control structure which makes the programmer to run a block of code for n number of times are until a specific condition is met.

Consider example if you want to sum numbers up to 100 then if you are writing each number something of it it’s a tedious process hence you can use a loop and perform the operation in a single line of code.

Another best example for loop is if you want to to read n numbers then you don’t have to use scanf n times simply you can put the scanf inside the loop.

Look at simple flowchart which of the flow of control of of a loop.
For any loop condition has to be specified. Until the condition is true the loop keeps executing the block of code. Whenever the condition is false the loop gets exited.

Loops in c
Control flow of loops

Type of Loops in C

In C programming language there are three loops.

Sl.NoNameKeyword
1Forfor
2Whilewhile
3Do-Whiledo while

We can also distinguish on the basis of control.

  • Entry colntrolled loop.
  • Exit controlled loop.

We can say entry controlled loops are for loop and while loop. Do-while is exit controlled loop.


For Loop in C Programming

For loops in C is used for performing set of operations n number of times.

The syntax of the for loop is as below.

for ( intitialization ; condition ;increment/decrement or any other operation )
{
 .
 code
 .
}

The intitialization section is where you declare and initialise a loop variable. If you have already declared and initialise the variable simply leave it blank don’t forget to prefix ; condition.

The condition section. Here you define a condition this condition is evaluated if it is true then the code in the loop will gets executed else the loop will be exited.

The next section is increment or decrement or any other operation here you will setup rules such that it results in the proper exit of the loop.

For loop check the condition for every for every time it is entering the loop hence it is entry controlled loop.

For loop may contain other loops or a for loop.

The simple for loop program given below:

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("For loops in c\n");
	//for loop example
	for( int i=0 ; i<10 ; i++ )
	{
		printf("%d\t",i);
	}
	printf("\n");
	return 0;
}
for loops in c
For loops in c

While Loops in C

While loop in C provides the programmer to to run a a set of code until a specific condition is met.

If there is no Exit condition defined in the loop then it will lead to to an infinite loop. Here the program will not be executed from the loop and it will execute continuously. You have to manually stop the process to exit the infinite loop. Avoid such infinite loops for good programming practice.

While loop check the condition for every for every time it is entering the loop hence it is entry controlled loop.

Syntax of while loop is as below:

while( condition )
{
 .
 code
 .
}

Take a look at a simple program for while loop:

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("While loops in c\n");
	//while loop example
	int i=0;
	while( i<10 )
	{
		printf("%d\t",i);
		i++;
	}
	printf("\n");
	return 0;
}
while loops in c
While loops in c

Do While Loops in C

We have in the while loops unlike while loops do while loop do not check for the entry condition but it checks while exiting the loop.

Do while loop is an exit controlled loop because it checks the condition whenever it is exiting from the loop.

If you put the same condition that you have put in a while loop do while executes one more time extra than while loop.

Syntax for do while loop is like below:

do
{
 .
 code
 .
}while ( condition ) ;

After specifying the condition you have to you specify the ; otherwise it will throw a compiler error.

Take a look at a simple do-while program.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf(" do While loops in c\n");
	// do while loop example
	int i=0;
	do
	{
		printf("%d\t",i);
		i++;
	}while( i<10 );
	printf("\n");
	return 0;
}
do while loop in c
Do while loop in c

Control Statements for Loops in C

C programming language provides various control statements to control a loop.

Look at the control statements table below.

Sl NoNamekeyword
1breakbreak
2continuecontinue
2gotogoto

Break keyword terminates the while loop. It has to be used inside the loop.

Continue keyword skips the set of code after its occurrence and moves to the next iteration.

Go to statement jump to a label specified.


Nested Loops in c

You may have already encountered a loop inside a loop this is called as nested loop.
While inputting a two dimensional array or inputting a matrix or if you are outputting the same then we are using a loop inside a loop right? Here in such conditions nesting loops are used.

You can nest any type of loop inside a loop that is also a type of nested loop.

Look at the simple example below for nested loops.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("For loops in c\n");
	//nested loop example
	for( int i=0 ; i<5 ; i++ )
	{
		//this is nesting of loops
		for( int j=0 ; j<5 ; j++ )
		{
			printf("%d\t",i);
		}
		printf("\n");
	}
	printf("\n");
	return 0;
}
Nested loops in c
Nested loops in c

Infinite Loops in c

If the condition of the loop never becomes false then it will lead to an infinite loop.

Infinite loops executes until the user terminates the program.

Conditions for infinite loops

  • Loop conditions never met. It means the condition is always true.
  • If there is no termination condition.

Please take a look at our simple program to to infinite loops in C.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf(" Infinite loops in c\n");
	// Infinite loop example
	int i=1;
	while( i );
	{
		
		
	}
	printf("\n");
	return 0;
}
Infinite loops in c
Infinite loops in c