Chapter 7: Loops in C
C gives us three main types of loops:
- for loop
- while loop
- do…while loop
We will also learn nested loops, break, and continue.
1. for Loop
Best when you know how many times you want to repeat (fixed number of times).
Syntax:
|
0 1 2 3 4 5 6 7 8 |
for (initialization; condition; update) { // code to repeat } |
- Initialization: Runs once (usually declare and set counter)
- Condition: Checked before every iteration
- Update: Runs after every iteration (usually increment/decrement)
Example: Print numbers 1 to 10
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } printf("\n"); return 0; } |
Output:
|
0 1 2 3 4 5 6 |
1 2 3 4 5 6 7 8 9 10 |
Another Example: Sum of first 10 natural numbers
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i, sum = 0; for (i = 1; i <= 10; i++) { sum = sum + i; } printf("Sum of first 10 numbers = %d\n", sum); return 0; } |
Output:
|
0 1 2 3 4 5 6 |
Sum of first 10 numbers = 55 |
2. while Loop
Best when you don’t know exactly how many times the loop will run – you repeat while a condition is true.
Syntax:
|
0 1 2 3 4 5 6 7 8 |
while (condition) { // code to repeat } |
Example: Keep asking for password until correct
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int password = 1234; int user_input; printf("Enter password: "); scanf("%d", &user_input); while (user_input != password) { printf("Wrong password! Try again: "); scanf("%d", &user_input); } printf("Access granted! Welcome.\n"); return 0; } |
3. do…while Loop
Similar to while, but guarantees at least one execution – code runs first, then condition is checked.
Syntax:
|
0 1 2 3 4 5 6 7 8 |
do { // code to repeat } while (condition); |
Example: Menu that keeps showing until user exits
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main() { int choice; do { printf("\n=== Menu ===\n"); printf("1. Add\n"); printf("2. Subtract\n"); printf("3. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); if (choice == 1) printf("You chose Add!\n"); if (choice == 2) printf("You chose Subtract!\n"); } while (choice != 3); printf("Thank you! Goodbye.\n"); return 0; } |
Key Difference:
- while → checks condition first (may never run)
- do…while → runs at least once, checks condition after
4. Nested Loops
A loop inside another loop – very useful for tables, patterns, 2D arrays, etc.
Example: Print multiplication table from 1 to 5
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main() { int i, j; for (i = 1; i <= 5; i++) { // Outer loop (rows) for (j = 1; j <= 10; j++) { // Inner loop (columns) printf("%d × %d = %2d ", i, j, i * j); } printf("\n"); // New line after each row } return 0; } |
Output (partial):
|
0 1 2 3 4 5 6 7 8 |
1 × 1 = 1 1 × 2 = 2 1 × 3 = 3 ... 2 × 1 = 2 2 × 2 = 4 2 × 3 = 6 ... ... |
5. break and continue Statements
break → Immediately stops the loop and jumps out
continue → Skips the current iteration and goes to next one
Example: Print numbers 1 to 10, but skip 5 and stop at 8
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { if (i == 5) { continue; // Skip 5 } if (i == 8) { break; // Stop loop at 8 } printf("%d ", i); } printf("\nLoop ended.\n"); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
1 2 3 4 6 7 Loop ended. |
Summary Table – When to Use Which Loop
| Loop Type | Best For | Checks Condition | Runs at Least Once? |
|---|---|---|---|
| for | Known number of times | Before | No |
| while | Unknown number of times | Before | No |
| do…while | Unknown, but must run at least once | After | Yes |
Today’s Homework
- Write a program using for loop to print all even numbers from 1 to 50.
- Write a while loop program that keeps asking the user for numbers until they enter 0 – then print the sum of all entered numbers.
- Create a do…while loop that shows a calculator menu (add, subtract, multiply, divide, exit) and performs operations until user chooses exit.
- Print this pattern using nested loops:
text012345678910***************
- Write a program that prints numbers 1 to 20, but skips multiples of 3 (use continue) and stops if number reaches 15 (use break).
