Do you think IIT Guwahati certified course can help you in your career?
No
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 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:
while loop
do-while loop
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
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;
}
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;
}
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;
}
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 Statement
Description
Example
break
Exits the loop immediately and transfers control to the statement following the loop.
c<br>if (condition) break;
continue
Skips the remaining code in the current iteration and moves to the next iteration of the loop.
c<br>if (condition) continue;
goto
Transfers 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?
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!!