24 Computer -- Programming Concepts and Logics

What is looping? What are the different types of looping used in C language?

What is looping? What are the different types of looping used in C language?

Looping is the process of continually repeating a sequence of instructions until a certain condition specified is reached.




The types of loops  in C programming languages are:


1) While Loop

. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

Following program illustrates while loop in C programming example:

#include<stdio.h> #include<conio.h> int main() { 	int num=1;	//initializing the variable 	while(num<=10)	//while loop with condition 	{ 		printf("%d\n",num); 		num++;		//incrementing operation 	} 	return 0; } 

Output:

1 2 3 4 5 6 7 8 9 10


2) Do-While

A do-while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.

Syntax of do while loop in C programming language is as follows:


In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;).


The following loop program in C illustrates the working of a do-while loop:

Below is a do-while loop in C example to print a table of number 2:

#include<stdio.h> #include<conio.h> int main() { 	int num=1;	//initializing the variable 	do	//do-while loop  	{ 		printf("%d\n",2*num); 		num++;		//incrementing operation 	}while(num<=10); 	return 0; } 

Output:

2 4 6 8 10 12 14 16 18 20


3) For loop

A for loop is a more efficient loop structure in ‘C’ programming. The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.


 The general structure of for loop syntax in C is as follows:

for (initial value; condition; incrementation or decrementation )  {   statements; } 

Following program illustrates the for loop in C programming example:

#include<stdio.h> int main() { 	int number; 	for(number=1;number<=10;number++)	//for loop to print 1-10 numbers 	{ 		printf("%d\n",number);		//to print the number 	} 	return 0; } 

Output:

1 2 3 4 5 6 7 8 9 10

More questions on Programming Concepts and Logics

Close Open App