Loops in C (for, while, do-while)
Loops allow a program to run a block of code multiple times automatically.
They reduce repetition and make logic cleaner and faster.
Why Loops?
- Repeat actions without writing code again and again
- Process lists, tables, and patterns
- Reduce time and code complexity
- Useful for calculations and automation
Types of Loops in C
1. for loop 2. while loop 3. do-while loop
1. for Loop
Used when the number of iterations is known.
for(initialization; condition; update){
// code
}
Example – Print 1 to 5
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++){
printf("%d\n", i);
}
return 0;
}
Example – Sum of First 10 Numbers
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1; i <= 10; i++){
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Example – Print Even Numbers (1-20)
#include <stdio.h>
int main() {
for(int i = 2; i <= 20; i += 2){
printf("%d ", i);
}
return 0;
}
2. while Loop
Used when the number of iterations is not known in advance.
while(condition){
// code
}
Example – Print 1 to 5
#include <stdio.h>
int main() {
int i = 1;
while(i <= 5){
printf("%d\n", i);
i++;
}
return 0;
}
Example – Countdown from 10 to 1
#include <stdio.h>
int main() {
int n = 10;
while(n >= 1){
printf("%d\n", n);
n--;
}
return 0;
}
Example – Read Until Negative Number
#include <stdio.h>
int main() {
int n;
printf("Enter numbers (negative to stop):\n");
while(1){
scanf("%d", &n);
if(n < 0)
break;
printf("You entered: %d\n", n);
}
return 0;
}
3. do-while Loop
Special loop: code executes **at least once**, even if condition is false.
do{
// code
} while(condition);
Example – Print 1 to 5
#include <stdio.h>
int main() {
int i = 1;
do{
printf("%d\n", i);
i++;
}while(i <= 5);
return 0;
}
Example – Ask for Password Until Correct
#include <stdio.h>
int main() {
int pass;
do{
printf("Enter password: ");
scanf("%d", &pass);
}while(pass != 1234);
printf("Access Granted!");
return 0;
}
Example – Always Executes Once
#include <stdio.h>
int main() {
int x = 10;
do{
printf("This will print even if x > 5\n");
}while(x < 5);
return 0;
}
Infinite Loops
A loop that never ends unless a break is used.
while(1){
printf("Running...\n");
}
For Loop – Example 1 (Print squares 1 to 10)
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++){
printf("%d squared = %d\n", i, i*i);
}
return 0;
}
For Loop – Example 2 (Reverse order 50 to 1)
#include <stdio.h>
int main() {
for(int i = 50; i >= 1; i--){
printf("%d ", i);
}
return 0;
}
For Loop – Example 3 (Print table of 7)
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++){
printf("7 x %d = %d\n", i, 7*i);
}
return 0;
}
For Loop – Example 4 (Count even & odd numbers)
#include <stdio.h>
int main() {
int even = 0, odd = 0;
for(int i = 1; i <= 20; i++){
if(i % 2 == 0)
even++;
else
odd++;
}
printf("Even: %d, Odd: %d", even, odd);
return 0;
}
For Loop – Example 5 (Sum of multiples of 3)
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 3; i <= 30; i += 3){
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
While Loop – Example 1 (Print digits of a number)
#include <stdio.h>
int main() {
int n = 54321;
while(n > 0){
printf("%d\n", n % 10);
n /= 10;
}
return 0;
}
While Loop – Example 2 (Sum until user enters 0)
#include <stdio.h>
int main() {
int n, sum = 0;
while(1){
scanf("%d", &n);
if(n == 0)
break;
sum += n;
}
printf("Total Sum = %d", sum);
return 0;
}
While Loop – Example 3 (Find factorial)
#include <stdio.h>
int main() {
int n = 5, fact = 1;
while(n > 0){
fact *= n;
n--;
}
printf("Factorial = %d", fact);
return 0;
}
While Loop – Example 4 (Print ASCII characters)
#include <stdio.h>
int main() {
int ch = 65;
while(ch <= 90){
printf("%c ", ch);
ch++;
}
return 0;
}
While Loop – Example 5 (Find smallest digit)
#include <stdio.h>
int main() {
int n = 75329;
int min = 9;
while(n > 0){
int d = n % 10;
if(d < min)
min = d;
n /= 10;
}
printf("Smallest digit = %d", min);
return 0;
}
Do-While – Example 1 (Print menu until exit)
#include <stdio.h>
int main() {
int choice;
do{
printf("1.Add 2.Sub 3.Exit\n");
scanf("%d", &choice);
}while(choice != 3);
return 0;
}
Do-While – Example 2 (Password retry)
#include <stdio.h>
int main() {
int pass;
do{
printf("Enter password: ");
scanf("%d", &pass);
}while(pass != 1234);
printf("Access Granted!");
return 0;
}
Do-While – Example 3 (Print 1 to 10)
#include <stdio.h>
int main() {
int i = 1;
do{
printf("%d ", i);
i++;
}while(i <= 10);
return 0;
}
Do-While – Example 4 (Always executes once)
#include <stdio.h>
int main() {
int x = 100;
do{
printf("This runs once!\n");
}while(x < 10);
return 0;
}
Do-While – Example 5 (Number guessing game)
#include <stdio.h>
int main() {
int guess;
do{
printf("Guess number (10): ");
scanf("%d", &guess);
}while(guess != 10);
printf("Correct Guess!");
return 0;
}
Difference Between Loops
for loop: - Used when repetition count is known while loop: - Used when repetition count is unknown do-while loop: - Executes at least once
Practice Questions
- Write a program to print the multiplication table of any number.
- Write a program to count digits of a number.
- Print numbers from 100 to 1 using a loop.
- Use a while loop to reverse a number.
- Use a do-while loop to create a login system.
Practice Task
Write a program to find the factorial of a number using:
• for loop
• while loop
• do-while loop
Print results of all three methods.