C Nested Loops
🔁🔁 C Nested Loops (Beginner → Advanced)
In C language, nested loops mean one loop inside another loop.
They are mainly used for patterns, matrices, tables, searching, and DSA problems.
1️⃣ What is a Nested Loop?
When a loop runs inside another loop, it is called a nested loop.
📌 Outer loop controls rows
📌 Inner loop controls columns
2️⃣ Basic Nested for Loop Example ⭐
Output
3️⃣ Nested Loop Execution Flow (Very Important)
✔ Inner loop runs completely for each outer loop iteration
✔ Total executions = 3 × 3 = 9
4️⃣ Nested Loops with Different Loop Types
for inside while
✔ Any loop can be nested inside another
5️⃣ Pattern Programs (Most Common Use) ⭐
🔹 Right-Angle Triangle
🔹 Number Pattern
6️⃣ Nested Loops with Arrays ⭐
2D Array Traversal
✔ Used in matrix operations
7️⃣ break in Nested Loops 🔥
✔ break exits only inner loop
8️⃣ continue in Nested Loops
✔ Skips only j == 2
9️⃣ Time Complexity of Nested Loops ⭐
| Loops | Complexity |
|---|---|
| 1 loop | O(n) |
| 2 nested loops | O(n²) |
| 3 nested loops | O(n³) |
📌 Very important in DSA & interviews
🔟 Common Mistakes ❌
❌ Wrong loop bounds
❌ Infinite loops
❌ Misplaced break / continue
❌ Forgetting newline after inner loop
📌 Interview Questions (Must Prepare)
-
What is nested loop?
-
How many times inner loop runs?
-
Effect of
breakin nested loop? -
Time complexity of nested loops?
-
Can
whilebe nested insidefor?
🔥 Real-Life Use Cases
-
Matrix calculations
-
Pattern printing
-
Multiplication tables
-
Grid-based problems
-
Image processing
✅ Summary
✔ Nested loops = loop inside loop
✔ Inner loop runs fully per outer iteration
✔ Commonly used with 2D arrays
✔ Pattern programs rely on nested loops
✔ Critical for DSA & interviews
