In programming, loops are used to repeat a block of code multiple times. Instead of writing the same statement ten times, you use a loop to tell the computer: "Execute this block until a certain condition is met." This concept is known as iteration.
Every loop, regardless of its type, consists of four essential elements:
Initialization: Setting a starting value for a counter variable (e.g., $i = 0$).
Condition: A logical expression that is checked before (or after) each iteration. If it’s true, the loop continues; if false, the loop terminates.
Body: The actual code you want to repeat.
Update (Increment/Decrement): Changing the counter variable so that the condition eventually becomes false (preventing an "Infinite Loop").
The while loop is the most basic looping structure. It is called an Entry-Controlled loop because it checks the condition before executing the body.
Theory: If the condition is false at the very beginning, the body of the loop will never execute. It is best used when you do not know exactly how many times the loop should run (e.g., reading a file until the end).
Syntax:
while (condition) { // code to repeat // update statement }
The for loop is a compact version of the while loop. It gathers initialization, condition, and update into a single line.
Theory: Like the while loop, it is entry-controlled. However, it is preferred when the number of iterations is known beforehand (e.g., "Repeat 10 times"). It is the most commonly used loop in C due to its readability.
Syntax:
for (initialization; condition; update) { // code to repeat }
The do-while loop is unique because it is an Exit-Controlled loop.
Theory: It executes the body of the loop first, and then checks the condition. This guarantees that the code inside the loop will execute at least once, even if the condition is false from the start.
Note: Unlike the other loops, a do-while statement must end with a semicolon ; after the condition.
Syntax:
do { // code to repeat } while (condition);
| Feature | for Loop | while Loop | do-while Loop |
| Control Type | Entry-Controlled | Entry-Controlled | Exit-Controlled |
| Minimum Iterations | 0 | 0 | 1 |
| Best Use Case | When iterations are known. | When iterations are unknown. | Menu-driven programs. |
| Syntax Style | Compact (3-in-1 line). | Spread out. | Spread out. |
This program calculates the sum of numbers using all three loops to demonstrate their different structures.
#include <stdio.h> int main() { int i; int sum_for = 0, sum_while = 0, sum_do = 0; // 1. For Loop for (i = 1; i <= 5; i++) { sum_for += i; } // 2. While Loop i = 1; // Re-initialization while (i <= 5) { sum_while += i; i++; // Update } // 3. Do-While Loop i = 1; // Re-initialization do { sum_do += i; i++; } while (i <= 5); printf("Sum using for: %d\n", sum_for); printf("Sum using while: %d\n", sum_while); printf("Sum using do-while: %d\n", sum_do); return 0; }
Infinite Loops: If a condition never becomes false (e.g., while(1)), the loop runs forever. This is useful in embedded systems (like an Arduino's loop()) but usually a bug in standard software.
Break Statement: Used to exit a loop immediately, regardless of the condition.
Continue Statement: Skips the current iteration and jumps straight to the next update/condition check.
Nested Loops: A loop inside another loop (common for working with 2D arrays or patterns).
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION