Table of Contents
Types of Loops in C
There are two types of loops in C based on when the loop condition is evaluated:Entry-Controlled Loop
In an entry-controlled loop, the condition is checked before the execution of the loop body. If the loop condition is false initially, then the loop body will not execute even once. Examples are the for loop and while loop.Exit-Controlled Loop
In an exit-controlled loop, the condition is checked after the loop body has executed. This ensures that the loop body runs at least once, regardless of the loop condition. Examples include the do-while loop.The for Loop
The for loop is an entry-controlled loop. This means that the condition is checked before the loop body executes. It is typically used when the number of iterations is known in advance. For example, traversing arrays, generating series, etc.Syntax:
Here,for (initialization; condition; increment/decrement) {
// loop body
}
- Initialization: Sets the starting value of the loop variable.
- Condition: Evaluated before each iteration; the loop continues as long as this is true.
- Increment/Decrement: Updates the loop variable after each iteration.
- Initialization executes once at the start.
- The condition is evaluated.
- If true, the loop body runs.
- If false, the loop terminates.
- After the loop body, the increment/decrement executes.
- Steps 2 and 3 repeat until the condition becomes false.
// C program to show for loop
#include stdio.h
// Main function
int main()
{
int i;
for (i = 1; i = 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
5
The while Loop
The while loop is an entry-controlled loop. If the condition is false at the very beginning, the loop body will run even once. It is commonly used when the number of iterations is known beforehand, and the loop continues until a particular condition is satisfied.Syntax:
Here,while (condition) {
// loop body
}
- Condition: Evaluated before every iteration. If true, the loop body executes; if false, the loop terminates.
- Loop body: Contains the set of statements that will repeat as long as the condition is true.
- The condition is evaluated.
- If true, the loop body executes.
- If false, the loop ends immediately.
- After executing the loop body, control goes back to check the condition again.
- Steps 1 and 2 repeat until the condition becomes false.
// C program to show while loop
#include stdio.h
// Main function
int main()
{
int i = 1;
while (i = 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
The do-while Loop
The do-while loop is an exit-controlled loop. This makes it different from the while and for loops, which may not run at all if the condition is false initially. It is especially useful when the program must execute the loop body first and then decide whether to continue.Syntax:
Here,do {
// loop body
} while (condition);
- Loop body: Executes once unconditionally.
- Condition: Evaluated after the loop body. If true, the loop continues, and if false, the loop terminates.
- The loop body executes first.
- The condition is then checked.
- If true, control goes back to the loop body.
- If false, the loop ends.
- This process repeats until the condition becomes false.
// C program to show do-while loop
#include stdio.h
// Main function
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
} while (i = 5);
return 0;
}
Output:
1
2
3
4
5
Nested Loops
A nested loop is a loop that is placed inside another loop. This allows repeated execution of one loop for every single execution of the outer loop. The for, while and do-while loops can be nested within each other, giving programmers flexibilityto handle complex repetitive tasks.Syntax:
Here,for (initialization; condition; increment) {
for (initialization; condition; increment) {
// inner loop body
}
// outer loop body
}
- Outer Loop: It controls the number of times the inner loop runs.
- Inner Loop: It executes completely for every single iteration of the outer loop.
// C program to show nested loops
#include stdio.h
// Main function
int main()
{
int i, j;
for (i = 1; i = 3; i++)
{
for (j = 1; j = 3; j++)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output:
Example 2: Multiplication Table1 2 3
1 2 3
1 2 3
// C program to show nested loops
#include stdio.h
// Main function
int main()
{
int i, j;
for (i = 1; i = 3; i++)
{
for (j = 1; j = 3; j++)
{
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}
return 0;
}
Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
Loop Control Statements
Loop control statements allow to skip iterations, terminate loops early, or transfer control explicitly, making programs more flexible and efficient.break statement
The break statement immediately terminates the loop and transfers control to the first statement after the loop.
Example:
// C program to show break statement
#include stdio.h
// Main function
int main()
{
int i;
for (i = 1; i = 5; i++)
{
if (i == 3)
{
// exits the loop when i is 3
break;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
The continue Statement
The continue statement skips the current iteration and moves control to the next iteration of the loop.Example:
// C program to show continue statement
#include stdio.h
// Main function
int main()
{
int i;
for (i = 1; i = 5; i++)
{
if (i == 3)
{
// skips printing when i is 3
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
4
5
Goto Statement
The goto statement allows an unconditional jump to a labelled part of the program. It is discouraged in structured programming, as it makes code harder to read and debug.Example:
// C program to show goto statement
#include stdio.h
// Main function
int main()
{
int i = 1;
loop:
if (i = 5)
{
printf("%d\n", i);
i++;
// jumps back to the label "loop"
goto loop;
}
return 0;
}
Output:
1
2
3
4
5
Infinite Loop
An infinite loop is a loop that never terminates because its condition always remain true or no terminating condition is provided. While often caused by programming errors, they can be used deliberately in certain applications like operating systems, embedded systems, or continuously running servers.Common Causes of Infinite Loops:
- Missing Update Statement: Forgetting to increment or decrement the loop variable.
- Incorrect Condition: Condition always evaluates to true.
- Logical Errors: Updating the wrong variable inside the loop.
// C program to show infinite loop
#include stdio.h
// Main function
int main()
{
int i = 1;
while (i = 5)
{
printf("%d\n", i);
// missing i++ causes infinite loop
}
return 0;
}
Example 2: Intentional Infinite Loop
// C program to show infinite loop
#include stdio.h
//Main function
int main()
{
// condition is always true
while (1)
{
printf("Running...\n");
// add break to stop intentionally
break;
}
return 0;
}
Comparison of Loops
Here is the comparison of for, while, and do-while loop:| Feature | for Loop | while Loop | do-while Loop |
|---|---|---|---|
| Type | Entry-controlled | Entry-controlled | Exit-controlled |
| Condition Check | Before loop body | Before loop body | After loop body |
| Initialization | Usually done in the loop header | Done outside the loop | Done outside the loop |
| Iteration Update | Specified in the loop header | Written inside the loop body | Written inside the loop body |
| Execution Count | May execute zero or more times | May execute zero or more times | Executes at least once |
| Best Use Case | When number of iterations is known | When number of iterations is unknown | When loop body must run at least once |
Conclusion
In conclusion, loops are powerful constructs that simplify repetitive tasks by reducing code duplication and improving clarity. With for, while, and do-while loop, programmers can choose the most efficient structure based on their problem. Loop control statements further enhance flexibility by allowing early exits or skipped iterations. Mastering loops builds a strong foundation for solving both simple and complex programming challenges.Frequently Asked Questions
1. What is a loop in C?2. What are the types of loops in C?A loop is used to execute a block of code repeatedly until a condition is met.
3. What is the difference between 'while' and 'do-while'?C has for, while, and do-while loops.
4. When should we use a for loop?while checks the condition before execution, while do-while runs at least once.
5. What causes an infinite loop?Use a for loop when the number of iterations is known.
Incorrect conditions or missing updates to the loop variable.
0 Comments