initially, the initialization statement is executed only once and statements(do part) execute only one. Then, the flow of control evaluates the test expression.
When the test expression is true, the flow of control enter the inner loop and codes inside the body of the inner loop is executed and updating statements are updated. This process repeats until the test expression is false(inner while loop)
Then, when the test expression is false, loop exit from the inner loop and flow of control comes to the outer loop. Again, the flow of control evaluates test condition.
If test condition for outer loop returns as true, the flow of control executes the body of while loop statements and return as false. The flow of control stops execution and goes to rest.
Examples for nested do-while loop
program 1
This program displays a square number pattern in C language
#include
#include
int main()
{
int i,j;
i=1;
printf("Square number pattern\n\n");
printf("Here your pattern\n\n");
do{
j=1;
do{
printf("%d",j);
j++;
}while(j
When the above code is executed, it produces the following results:
Square number pattern
Here your pattern
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
Program 2
This program displays a square star pattern in C language
#include
#include
int main()
{
int i,j;
i=1;
printf("Asterisk number pattern\n\n");
printf("Here your pattern\n\n");
do{
j=1;
do{
printf("*");
j++;
}while(j
When the above code is executed, it produces the following results:
Asterisk number pattern
Here your pattern
*********
*********
*********
*********
*********
*********
*********
*********
*********
The above program displays a square star pattern in C language using nested do-while loop
Program 3
This program displays a floyd’s triangle number pattern in C language using do-while loop
#include
#include
int main()
{
int i,j;
i=1;
printf("Triangle number pattern\n\n");
printf("Here your pattern\n\n");
do{
j=1;
do{
printf("%d",j);
j++;
}while(j
When the above code is executed, it produces the following results:
Triangle number pattern
Here your pattern
1
12
123
1234
12345
123456
1234567
12345678
123456789
This program displays a Floyd triangle number pattern using do-while in C language
Program 4
Print multiplication table using nested do-while loop in C
#include
#include
int main()
{
int i,j;
i=1;
printf("Multiplication table\n\n");
printf("Here multiplication table\n\n");
do{
j=1;
do{
printf("%d\t",i*j);
j++;
}while(j