What is Nested do while in C?
A nested do while loop in C is a loop that is enclosed within another do while loop. It is a loop inside a loop. In this loop structure, the inner loop executes multiple times for each iteration of the outer loop.
We use a nested do while loop in C to perform a specific task repeatedly. We can use it when we need to iterate through a set of data multiple times, with each iteration involving a different set of operations.
Syntax:
do {
// Outer loop statements
do {
// Inner loop statements
} while (condition);
} while (condition);
Here, the inner loop is executed until the given condition is true. Then, the outer loop is executed until its condition becomes false.
Examples of Nested do while loop in C:
1. Printing Multiplication Table from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
int j;
do {
j = 1;
do {
printf("%d * %d = %d\n", i, j, i*j);
j++;
} while(j <= 10);
i++;
} while(i <= 5);
return 0;
}
Output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
...
...
...
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Explanation:
In above example, the nested do while loop is used to print the multiplication table of numbers from 1 to 5. The outer loop runs from 1 to 5 and the inner loop runs from 1 to 10. The output displays the result of each multiplication table.
2. Printing a matrix based on the size given by user
#include <stdio.h>
int main() {
int i = 1;
int j = 1;
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
do {
do {
if(i == j) {
printf("%d ", i);
} else {
printf("0 ");
}
j++;
} while(j <= n);
printf("\n");
i++;
j = 1;
} while(i <= n);
return 0;
}
Output:
Enter the value ofn: 6
1 0 0 0 0 0
0 2 0 0 0 0
0 0 3 0 0 0
0 0 0 4 0 0
0 0 0 0 5 0
0 0 0 0 0 6
Explanation:
In above example, the nested do while loop is used to print a diagonal matrix of numbers from 1 to n. The outer loop runs from 1 to n and the inner loop runs from 1 to n. The output displays a matrix with all values set to 0, except for the diagonal, which is set to the corresponding number.
3. Printing a Box of Asterisks
#include <stdio.h>
int main() {
int i = 1, j = 1, n = 5;
do {
do {
if(j==1 || j==n || i==1 || i==n) {
printf("* ");
} else {
printf(" ");
}
j++;
} while(j <= n);
printf("\n");
i++;
j = 1;
} while(i <= n);
return 0;
}
Output:
* * * * *
* *
* *
* *
* * * * *
Explanation:
In above example, the nested do-while loop is used to print a hollow square of asterisks with sides of length n. The outer loop runs from 1 to n and the inner loop runs from 1 to n as well. The if-else statement is used to print an asterisk (*) if the current iteration is on the border of the square or to print a space ( ) if it is inside the square.
4. Some kind of sparkle
#include <stdio.h>
int main() {
int i = 1, j = 1, k = 1, n = 5;
do {
do {
k = (i + j) % 2;
if(k == 0) {
printf("* ");
}
else {
printf("# ");
}
j++;
} while(j <= i);
printf("\n");
i++;
j = 1;
} while(i <= n);
return 0;
}
Output:
*
# *
* # *
# * # *
* # * # *
Explanation:
In above example, the nested do-while loop is used to print a triangular pattern of alternating asterisks (*) and hashes (#) with a base of length n. The outer loop runs from 1 to n, and the inner loop runs from 1 to i. The variable k is used to keep track of whether an asterisk or hash should be printed at each iteration. The output displays the triangular pattern of alternating asterisks and hashes with a base of length 5.
Rules to follow when using nested do while loop in C:
- Ensure that the loop conditions are set correctly for both the outer and inner loops.
- Use appropriate variables and expressions within the loop.
- Ensure that the inner loop is properly nested within the outer loop.
- Make sure to use proper indentation to make the code more readable.