Chapter 5 – Loops in C Language
Introduction
In programming, we often need to perform the same task multiple times. Writing the same statements again and again would make a program longer and harder to manage. Loops provide a simple way to repeat a set of statements until a particular condition is satisfied.
For example, if we want to print numbers from 1 to 10, we could write ten printf() statements. However, using a loop, the same task can be completed with only a few lines of code.
In this chapter, you will learn about the different types of loops available in the C language and how to use them effectively.
1. What is a Loop?
A loop is a programming structure that executes a block of statements repeatedly as long as a specified condition is true.
Basic idea
Start ↓ Check Condition ↓ Execute Statements ↓ Update ↓ Check Condition again
When the condition becomes false, the loop stops.
2. Types of Loops in C
C language mainly provides three types of loops:
for loop
while loop
do-while loop
Comparison
| Loop | Condition Checked | Best Used When |
|---|---|---|
| for | Before execution | Number of repetitions is known |
| while | Before execution | Number of repetitions may not be known |
| do-while | After execution | Statements must execute at least once |
3. The for Loop
The for loop is commonly used when we know approximately how many times a block of code should execute.
Syntax
for(initialization; condition; update) { statements; }
The three important parts are:
Initialization – sets the starting value.
Condition – determines whether the loop should continue.
Update – changes the loop variable after each iteration.
Example
#include <stdio.h> int main() { int i; for(i = 1; i <= 5; i++) { printf("%d\n", i); } return 0; }
Output
1 2 3 4 5
How it works
Initially:
i = 1
The condition i <= 5 is checked. If it is true, the printf() statement executes. Then i++ increases the value of i by 1.
This continues until the condition becomes false.
4. Printing Numbers Using a for Loop
The following program prints numbers from 1 to 10.
#include <stdio.h> int main() { int i; for(i = 1; i <= 10; i++) { printf("%d ", i); } return 0; }
Output
1 2 3 4 5 6 7 8 9 10
5. Printing Even Numbers
A loop can also be used to print even numbers.
#include <stdio.h> int main() { int i; for(i = 2; i <= 20; i = i + 2) { printf("%d ", i); } return 0; }
Output
2 4 6 8 10 12 14 16 18 20
6. The while Loop
The while loop executes a block of statements as long as its condition remains true.
Syntax
while(condition) { statements; }
Example
#include <stdio.h> int main() { int i = 1; while(i <= 5) { printf("%d\n", i); i++; } return 0; }
Output
1 2 3 4 5
Important Point
In a while loop, the condition is checked before the statements are executed.
Therefore, if the condition is false at the beginning, the loop body will not execute even once.
7. Example of while Loop
The following program prints a message five times.
#include <stdio.h> int main() { int count = 1; while(count <= 5) { printf("Welcome to C Programming\n"); count++; } return 0; }
The count++ statement is important because it changes the value of count. Without an appropriate update, the loop may continue indefinitely.
8. The do-while Loop
The do-while loop is different from the for and while loops because its condition is checked after the loop body executes.
Syntax
do { statements; } while(condition);
Example
#include <stdio.h> int main() { int i = 1; do { printf("%d\n", i); i++; } while(i <= 5); return 0; }
Output
1 2 3 4 5
9. Important Feature of do-while
A do-while loop executes its body at least once, even if the condition is initially false.
Example:
#include <stdio.h> int main() { int i = 10; do { printf("%d", i); } while(i < 5); return 0; }
Output
10
Although i < 5 is false, the statement executes once before the condition is checked.
10. Difference Between while and do-while
Consider the following two programs.
while Loop
int i = 10; while(i < 5) { printf("%d", i); }
The statement does not execute because the condition is false before entering the loop.
do-while Loop
int i = 10; do { printf("%d", i); } while(i < 5);
Here, the statement executes once because the condition is checked after the loop body.
11. Nested Loops
A loop inside another loop is called a nested loop.
For example:
#include <stdio.h> 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
1 2 3 1 2 3 1 2 3
Nested loops are commonly used for patterns, tables, matrices and other problems involving rows and columns.
12. Printing a Simple Pattern
Loops can be used to create different patterns.
#include <stdio.h> int main() { int i, j; for(i = 1; i <= 5; i++) { for(j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }
Output
* * * * * * * * * * * * * * *
13. Infinite Loop
A loop that never terminates is called an infinite loop.
Example:
while(1) { printf("Hello\n"); }
Since 1 is treated as true in C, the condition never becomes false.
Infinite loops can sometimes be useful in programs such as continuously running systems, but they should be used carefully.
14. break Statement
The break statement is used to immediately terminate a loop.
Example
#include <stdio.h> int main() { int i; for(i = 1; i <= 10; i++) { if(i == 6) { break; } printf("%d ", i); } return 0; }
Output
1 2 3 4 5
When i becomes 6, the break statement terminates the loop.
15. continue Statement
The continue statement skips the remaining statements of the current iteration and moves to the next iteration.
Example
#include <stdio.h> int main() { int i; for(i = 1; i <= 5; i++) { if(i == 3) { continue; } printf("%d ", i); } return 0; }
Output
1 2 4 5
The value 3 is skipped.
16. Loop Control Statements
The following statements are commonly used to control loops:
| Statement | Purpose |
|---|---|
| break | Terminates the loop |
| continue | Skips the current iteration |
| return | Exits the current function |
17. Sum of Numbers Using a Loop
Loops can be used to perform calculations repeatedly.
Example
Find the sum of numbers from 1 to 10.
#include <stdio.h> int main() { int i, sum = 0; for(i = 1; i <= 10; i++) { sum = sum + i; } printf("Sum = %d", sum); return 0; }
Output
Sum = 55
18. Multiplication Table
A for loop can be used to generate a multiplication table.
#include <stdio.h> int main() { int n, i; printf("Enter a number: "); scanf("%d", &n); for(i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } return 0; }
If the user enters 5, the program displays the multiplication table of 5.
19. Common Mistakes in Loops
1. Forgetting to update the loop variable
int i = 1; while(i <= 5) { printf("%d", i); }
Here, i never changes, so the loop does not reach its stopping condition.
2. Using the wrong condition
Always check whether the condition correctly represents the required number of repetitions.
3. Incorrect semicolon
Be careful with semicolons after loop conditions.
for(i = 1; i <= 5; i++); { printf("%d", i); }
The semicolon changes the behavior of the loop and may produce unexpected results.
20. for, while and do-while – Quick Revision
for Loop
Use it when the number of repetitions is generally known.
for(i = 1; i <= 10; i++) { printf("%d", i); }
while Loop
Use it when repetition depends mainly on a condition.
while(condition) { statements; }
do-while Loop
Use it when the statements should execute at least once.
do { statements; } while(condition);
21. Key Points to Remember
A loop is used to repeat statements.
C provides three main loops: for, while, and do-while.
The for loop is useful when the number of iterations is known.
The while loop checks its condition before execution.
The do-while loop checks its condition after execution.
A do-while loop executes at least once.
A loop inside another loop is called a nested loop.
break terminates a loop.
continue skips the current iteration.
Always ensure that the loop can eventually reach its terminating condition.
22. Practice Questions
Multiple Choice Questions
1. Which loop is generally suitable when the number of iterations is known?
A. if
B. for
C. switch
D. break
Answer: B. for
2. Which loop executes its body at least once?
A. for
B. while
C. do-while
D. if
Answer: C. do-while
3. Which statement is used to terminate a loop immediately?
A. stop
B. exit
C. break
D. continue
Answer: C. break
4. Which statement skips the current iteration?
A. break
B. continue
C. skip
D. goto
Answer: B. continue
5. What is a loop inside another loop called?
A. Infinite loop
B. Nested loop
C. Conditional loop
D. Sequential loop
Answer: B. Nested loop
23. Programming Exercises
Try writing C programs to:
Print numbers from 1 to 100.
Print all even numbers from 1 to 50.
Print all odd numbers from 1 to 50.
Find the sum of numbers from 1 to 100.
Generate a multiplication table for a number entered by the user.
Find the factorial of a number using a loop.
Count the digits of an integer.
Reverse an integer using a loop.
Print a star triangle using nested loops.
Find the largest number from a set of numbers using a loop.
Chapter Summary
Loops are one of the most important concepts in C programming. They allow programmers to repeat instructions efficiently instead of writing the same statements multiple times.
The three major loops are for, while, and do-while. Understanding their syntax, working principles, and appropriate uses provides a strong foundation for solving programming problems.
In the next chapter, we will build on these concepts and explore Arrays in C Language.