Table of contents
1.
Introduction 
2.
What are Loops in C?
3.
Types of loops
3.1.
Entry Controlled loops
3.2.
Exit Controlled Loops
4.
while loop
4.1.
Flow diagram
4.2.
Example 
5.
do-while loop
5.1.
Flow diagram
5.2.
Example
6.
for loop
6.1.
Flow diagram
6.2.
Example 
7.
Loop Control Statements
8.
Advantages of Loops in C
9.
Disadvantages of Loops in C
10.
Frequently asked questions
10.1.
What is an infinite loop in C?
10.2.
What is the syntax for running two variables for loop simultaneously?
10.3.
What is for loop in C?
11.
Conclusion
Last Updated: Dec 3, 2024
Easy

Loops in C

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In any programming language, including C, we use loops to repeatedly execute a set of statements until a particular condition is satisfied. For example, suppose we want to print "Coding Ninjas" ten times. One way to do this is to write ten printf statements, which is not preferable. Another way to do this is to use a loop. Using a loop, we can write one loop statement and only one printf statement, and this approach is better than the first one.

loops

Also Read, Sum of Digits in C, C Static Function

What are Loops in C?

Loops in C are control structures that allow you to repeatedly execute a block of code as long as a specified condition is true. They are commonly used to perform repetitive tasks, iterate through arrays, or handle data structures efficiently.

Types of loops

Based on the position of a control statement in a C program, There are mainly two types of loops:

1. Entry controlled loop

2. Exit controlled loop

Entry Controlled loops

In an entry control loop in C, the test condition is checked before executing the body of a loop. Entry-controlled loops are for loop and while loop.

Exit Controlled Loops

In an exit controlled loop in C, the test condition is checked after executing the body of a loop. Exit controlled loop is the do-while loop.

The C language provides us with three types of loop constructs:

  1. while loop
  2. do-while loop
  3. for loop

while loop

The syntax of the while loop is-

while(expression)
{
    // statements
}

In the while loop, first, expression is evaluated. If the expression is true, then and only then the body of a loop is executed. After the execution of the body of the loop, again expression is evaluated, and if it is found to be true again, then the body of a loop is executed. This means that the body of a loop is executed continuously till the expression is true. Once the expression becomes false, the loop terminates, and the control goes out of the loop.

Flow diagram

flow diagram

Example 

// C program to illustrate while loop
#include<stdio.h>
int main()
{
    int i=0;    // initializing the variable
    while (i<5)    // while loop with condition
    {
        printf("Coding Ninjas\n");
        i++;    // incrementing operation
    }
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas

do-while loop

The syntax of the do-while loop is-

do
{
  // statements
}while(expression);

In the do-while loop, first, the body of a loop is executed, and then the expression is evaluated. If the expression is true, then again, the loop's body is executed, and this process continues until the expression is true. When the expression becomes false, the loop terminates. In the while loop, the loop's body is executed only when the expression is true, whereas, in the do-while loop, the loop's body will be executed at least once, even if the expression is false.

Flow diagram

flow diagram

Example

// C program to illustrate do-while loop
#include<stdio.h>
int main()
{
    	int i=0; //initializing the variable
	do // do-while loop 
	{
    	printf("Coding Ninjas\n");
    	i++; // incrementing operation
	}while(i<5);
	return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas

 

You can also read about the jump statement, Tribonacci Series

for loop

The syntax of the for loop is-

for(expr1; expr2; expr3)
{
  // statements
}

In the above syntax, expr1 is an initialization expression, expr2 is a test expression or condition, and expr3 is an update expression. Firstly initialization expression is executed, and the loop variables are initialized, then the test expression is checked. Suppose the test expression(expr2) is true. In that case, the loop's body is executed. after executing the loop's body, control transfers to the update expression(expr3), it modifies the loop variables, and then again, the test expression(expr2) is checked. If the test expression(expr2) is true, the loop's body is executed again. This process continues till the test expression is true, and When the test expression becomes false, the loop terminates.

Flow diagram

flow diagram

Example 

// C program to illustrate do-while loop
#include<stdio.h>
int main()
{
    int i;
	for(i=0;i<5;i++) // for loop 
	{
    	printf("Coding Ninjas\n");
	}
	return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas


Also read,  Short int in C Programming

Loop Control Statements

Loop control statements in C are used to modify the normal flow of loops, allowing you to skip iterations or exit the loop prematurely. The following table summarizes these statements:

Control StatementDescriptionExample
breakExits the loop immediately and transfers control to the statement following the loop.c<br>if (condition) break;
continueSkips the remaining code in the current iteration and moves to the next iteration of the loop.c<br>if (condition) continue;
gotoTransfers control to a labeled statement within the code (rarely used and discouraged).c<br>goto label;

Advantages of Loops in C

  • Code Reusability: Allows a block of code to be reused multiple times without rewriting it.
  • Efficiency: Reduces manual coding for repetitive tasks, making the program concise and faster to write.
  • Scalability: Makes handling large datasets and complex computations more practical.
  • Dynamic Behavior: Enables programs to adapt to varying conditions dynamically.
  • Ease of Maintenance: Simplifies debugging and updates by minimizing redundant code.

Disadvantages of Loops in C

  • Complexity in Logic: Poorly written loops can be hard to understand and debug.
  • Infinite Loops: A common issue if the loop condition is not managed correctly, leading to unresponsive programs.
  • Performance Overhead: Nested or poorly optimized loops can increase execution time and reduce program efficiency.
  • Resource Consumption: Loops processing large datasets can consume significant memory and CPU resources.

Frequently asked questions

What is an infinite loop in C?

An infinite loop is a loop that repeats indefinitely and does not terminate. An infinite loop is also called an indefinite loop or an endless loop.

What is the syntax for running two variables for loop simultaneously?

Syntax:
for (initialization_expr1, initialization_expr1; test_expr1, test_expr2; update_expr1, update_expr2)
{
     // statements 
}

What is for loop in C?

The for loop in C language is a repetition control structure that allows you to efficiently write a loop that is executed a specific number of times.

Conclusion

In this blog, We learned about loops, types of loops, and how these loops work with the help of flow diagrams.

Recommended Readings:

This blog is over, but the thirst for learning is not over yet. If you want to take your learnings to the next level, you can visit and read our library of curated blogs by clicking here. Use our practice platform Code360 to practice various DSA questions asked in many interviews. Till then, have a nice day and Happy Coding!! 

Live masterclass