C Nested Loops
1. What is a Nested Loop?
-
A nested loop is a loop inside another loop.
-
The inner loop completes all its iterations for each iteration of the outer loop.
-
Syntax:
Nested loops can also be
whileinsidefor,forinsidewhile, orwhileinsidewhile.
2. Simple Nested for Loop Example
Print a 5×5 multiplication table (partial):
Output:
3. Printing Patterns
Right-Angle Triangle of *
Output:
Square Pattern of Numbers
Output:
4. Key Points About Nested Loops
-
Inner loop runs completely for every iteration of the outer loop.
-
Useful for patterns, matrices, tables, and multi-dimensional arrays.
-
Be careful: too many nested loops can slow down the program (time complexity increases).
-
Can mix different types of loops (
for,while,do...while) inside each other.
5. Example – Multiplication Table Using Nested Loops
Output:
