This is the 36th article in the C language learning series.
The body of a loop can contain any statement, including another loop statement.
A nested loop is when one loop is placed inside another loop. Just like in our daily lives, there are 7 days in a week (outer loop), and each day has 24 hours (inner loop), thus combining gives us a schedule of 7×24 hours.
In C language, the most commonly used are nested for loops and while loops.
Basic syntax structure
for(outer initialization; outer condition; outer update){
// Outer loop body
for(inner initialization; inner condition; inner update){
// Inner loop body
}
}
Classic example: Printing the multiplication table
Let’s understand nested loops through a classic example:
#include <stdio.h>
int main(){
int i, j;
for(i =1; i <=9; i++){ // Outer loop: controls the number of rows
for(j =1; j <= i; j++){ // Inner loop: controls the number of columns in each row
printf(“%d×%d=%-2d “, j, i, i * j);
}
printf(“\n”);// New line
}
return0;
}
Output:
text
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
… (and so on)
Example 2: Printing patterns
Nested loops are very suitable for printing various shapes:
#include <stdio.h>
int main(){
int rows =5;
// Print right-angled triangle
printf(“Right-angled triangle:\n”);
for(int i =1; i <= rows; i++){
for(int j =1; j <= i; j++){
printf(“* “);
}
printf(“\n”);
}
// Print inverted triangle
printf(“\nInverted triangle:\n”);
for(int i = rows; i >=1; i—){
for(int j =1; j <= i; j++){
printf(“* “);
}
printf(“\n”);
}
return0;
}
Example 3: Handling two-dimensional arrays
Nested loops are an excellent tool for handling two-dimensional arrays:
#include <stdio.h>
int main(){
int matrix[3][4]={
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf(“Contents of the two-dimensional array:\n”);
for(int i =0; i <3; i++){ // Traverse rows
for(int j =0; j <4; j++){ // Traverse columns
printf(“%d\t”, matrix[i][j]);
}
printf(“\n”);
}
// Calculate sum
int sum =0;
for(int i =0; i <3; i++){
for(int j =0; j <4; j++){
sum += matrix[i][j];
}
}
printf(“Sum of array elements: %d\n”, sum);
return0;
}
Example 4: Bubble Sort Algorithm
Nested loops are very important in sorting algorithms:
#include <stdio.h>
int main(){
int numbers[]={5, 2, 8, 1, 9};
int n =5;
printf(“Before sorting: “);
for(int i =0; i < n; i++){
printf(“%d “, numbers[i]);
}
// Bubble sort – using nested loops
for(int i =0; i < n –1; i++){ // Outer loop: controls the number of passes
for(int j =0; j < n –1– i; j++){// Inner loop: compares adjacent elements
if(numbers[j]> numbers[j +1]){
// Swap positions
int temp = numbers[j];
numbers[j]= numbers[j +1];
numbers[j +1]= temp;
}
}
}
printf(“\nAfter sorting: “);
for(int i =0; i < n; i++){
printf(“%d “, numbers[i]);
}
return0;
}
Important Notes
1.Variable naming: The outer loop is usually namedi, and the inner loop usesj,k, etc., to avoid confusion
2.Loop conditions: Ensure the inner loop’s condition is correct to avoid infinite loops
3.Performance considerations: The time complexity of nested loops is O(n²), which may be slow for large data sets
4.Indentation format: Maintain good code indentation to make the nested structure clear
Practice Suggestions
1.Try printing different shapes (diamonds, squares, etc.)
2.Implement matrix addition and multiplication using nested loops
3.Try other sorting algorithms (like selection sort)
4.Solve some logic problems, such as finding all prime numbers under 100
Summary
Nested loops are an important concept in C programming, allowing us to handle more complex data structures and algorithms. Remember: the outer loop controls the “big rhythm,” while the inner loop handles the “details.” Practice more and think critically, and you will soon master this powerful tool!
Programming is like building with blocks, and nested loops allow you to construct more complex and beautiful structures. Keep it up!