LOOP STATEMENTS IN C
Loops are control structures used in C programming to execute a block of code multiple times,
either based on a condition or a fixed number of iterations. Loops reduce code redundancy and
are essential for efficient programming.
1. Classification of Loop Statements
While Loop in C
The while loop is a control flow statement in C that allows code to be executed repeatedly
based on a given condition. It is an entry-controlled loop, meaning the condition is checked
before the loop body executes. If the condition evaluates to true (non-zero), the loop executes
the block of code. If it evaluates to false (zero), the loop terminates.
Syntax: while (condition)
// Code to execute repeatedly
Condition: A boolean expression that determines whether the loop continues.
The loop executes repeatedly until the condition becomes false.
How it Works
1. Step 1: The condition is evaluated.
2. Step 2: If the condition is true, the loop body executes.
3. Step 3: After executing the loop body, the condition is evaluated again.
4. Step 4: The process repeats until the condition is false.
Flowchart
1. Start → Check Condition → If True → Execute Loop Body → Repeat.
2. If False → Exit Loop → End.
Examples
Example 1: Simple Counter
Print numbers from 1 to 5 using a while loop.
#include <stdio.h>
int main()
int i = 1; // Initialization
while (i <= 5) // Condition
printf("%d\n", i); // Print current value of i
i++; // Increment
return 0;
Output: 1
Example 2: Sum of Natural Numbers
Calculate the sum of numbers from 1 to n.
#include <stdio.h>
int main()
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n)
sum += i; // Add i to sum
i++; // Increment i
printf("Sum = %d\n", sum);
return 0;
Input/Output:
Enter a positive integer: 5
Sum = 15
Example 3: Reverse Digits of a Number
Reverse the digits of a given number using a while loop.
#include <stdio.h>
int main()
int num, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0)
remainder = num % 10; // Get last digit
reverse = reverse * 10 + remainder; // Build reversed number
num /= 10; // Remove last digit
printf("Reversed Number = %d\n", reverse);
return 0;
Input/output: Enter an integer: 1234
Reversed Number = 4321
Example 4: Infinite Loop
A while loop runs infinitely when the condition always evaluates to true.
#include <stdio.h>
int main()
while (1)
{ // Always true
printf("This is an infinite loop.\n");
return 0;
Note: Use break to exit an infinite loop.
Example 5: Using break in a while Loop
Exit the loop when a specific condition is met.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
if (i == 5) {
b reak; // Exit the loop when i equals 5
printf("%d\n", i);
i++;
return 0;
Output: 1
Example 6: Using continue in a while Loop
Skip the current iteration when a condition is met.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++; // Increment to avoid infinite loop
continue; // Skip the rest of the iteration
}
printf("%d\n", i);
i++;
return 0;
Output: 1
Common Errors in while Loops
1. Infinite Loops:
o Forgetting to update the loop variable leads to infinite execution.
int i = 1;
while (i <= 5)
printf("%d\n", i);
// Missing i++
2. Logical Errors:
Incorrect condition can result in skipped or extra iterations.
int i = 1;
while (i = 5)
{ // Mistaken assignment instead of comparison
printf("%d\n", i);
}
Advantages of while Loop
Ideal for indeterminate loops, where the number of iterations is not known in advance
(e.g., reading user input until a specific value is entered).
Simple and flexible for dynamic conditions.
Disadvantages of while Loop
Higher chance of creating infinite loops if the loop variable is not updated properly.
May be less concise than a for loop for cases with a known iteration count.