In this tutorial, We will learn about Nested while loop in C programming language
In C programming language, while loop inside another while loop is known as nested while loop. In nested while loop one or more statements can be included in the body of the loop.
The syntax of nested while in C
while(test_expression) { statements; while(test_expression) { statements; } }
Initially, Outer while loop executes only once. outer while loop evaluates the test expression. When the condition is false, The flow of control skips the execution and flow of control come out the outer loop for rest when the condition is true, the flow of control jumps to the inner while loop.
Then, test expression evaluates the condition of the inner loop. if the test expression is true, inside the statements are executed If the test expression is false, the flow of control skips the execution and jumps out of the outer loop.
#include <stdio.h> #include <stdlib.h> int main() { int i=1; while(i<=3){ printf("%d :inner loop is executed only once\n",i); int j=1; while(j<=4){ printf("%d :Outer loop is executed until to completion\n",j); j++; } i++; } getch(); return 0; }
When the above code is compiled and executed, it produces the following result:
1 :inner loop is executed only once 1 :Outer loop is executed until to completion 2 :Outer loop is executed until to completion 3: Outer loop is executed until to completion 4: Outer loop is executed until to completion 1 :inner loop is executed only once 1 :Outer loop is executed until to completion 2 :Outer loop is executed until to completion 3: Outer loop is executed until to completion 4: Outer loop is executed until to completion 1 :inner loop is executed only once 1 :Outer loop is executed until to completion 2 :Outer loop is executed until to completion 3: Outer loop is executed until to completion 4: Outer loop is executed until to completion
Program 1
Rectangular number pattern printing using nested while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int i=1,j; while(i<=10){ j=1; while(j<=10){ printf("%d",j); j++; } printf("\n"); i++; } getch(); return 0; }
When the above code is compiled and executed, it produces the following result:
12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
Program 3
Rectangular star pattern printing using nested while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int i=1,j; while(i<=10){ j=1; while(j<=10){ printf("*"); j++; } printf("\n"); i++; } getch(); return 0; }
When the above code is compiled and executed, it produces the following result:
********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Program 3
#include <stdio.h> #include <stdlib.h> int main() { printf("Multiplication table\n\n"); int i=1,j; while(i<=10){ j=1; while(j<=10){ printf("%d\t",j*i); j++; } printf("\n"); i++; } getch(); return 0; }
When the above code is compiled and executed, it produced the following result.
multiplication table printing using nested while loop in C language
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
#include <stdio.h> #include <stdlib.h> int main() { const char* names[3][4]={ {"Nimal","Kumar","Sugu","Thibi"}, {"Puvi","Khan","Jhon","Sumi"}, {"Kabil","Kuna","Kuru","Puvi"}, }; int i=0; while(i<=2){ int j=0; while(j<=3){ printf("%s\n",names[i][j]); j++; } i++; } printf("End the program\n"); getch(); return 0; }
When the above code is compiled and executed, it produced the following result.
Nimal Kumar Sugu Thibi Pavi Khan Jhon Sumi Kabil Kuna Puvi End the program
Know more about Array
Array in Programming languages
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…