Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Types of loops
2.1.
Entry Controlled loops
2.2.
Exit Controlled Loops
3.
while loop
4.
do-while loop
5.
for loop
6.
Frequently asked questions
7.
Key takeaways
Last Updated: Mar 27, 2024
Easy

Loops

Author Shivam Verma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

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

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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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;
}

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

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;
}

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

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;
}

Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas


Also read,  Short int in C Programming

Frequently asked questions

  1. What is an infinite loop in C?
    Ans: 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.
     
  2. What is the syntax for running two variables for loop simultaneously?
    Ans: Syntax:
    for (initialization_expr1, initialization_expr1; test_expr1, test_expr2; update_expr1, update_expr2)
    {
         // statements 
    }
     
  3. What is for loop in C?
    Ans: 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.

Key takeaways

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 Coding Ninjas Studio to practice various DSA questions asked in many interviews. Till then, have a nice day and Happy Coding!! 

Previous article
Control Structures in C
Next article
Jump Statement
Live masterclass