Nested do while loop in C language
- Home
- nested while loop
- Nested do while loop in C language
- On
- By
- 0 Comment
- Categories: nested while loop
Nested do while loop in C language
Nested do while loop in C language
In this tutorial, we will learn about Nested do while loop in C programming language
In C programming language, one do-while inside another do-while is known as nested do -while loop
Declaration
Syntax
statements do{ statements do{ statements }while(condition); statements }while(condition);
Flow diagram of the Nested Do-while loop in C
How to work Nested do while loop
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 <stdio.h> #include <stdlib.h> 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<=10); printf("\n"); i++; }while(i<=10); getch(); return 0; }
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 <stdio.h> #include <stdlib.h> 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<=9); printf("\n"); i++; }while(i<=9); getch(); return 0; }
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 <stdio.h> #include <stdlib.h> 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<=i); printf("\n"); i++; }while(i<=9); getch(); return 0; }
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 <stdio.h> #include <stdlib.h> 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<=10); printf("\n"); i++; }while(i<=10); getch(); return 0; }
Suggested for you
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using for loop in Java
Floyd’s triangle pattern using while loop in Java
Hollow pyramid triangle pattern in C++ language
Similar post
Nesting for in Python language