In this tutorial, We will learn about Nested while loop in Cpp programming language
While loop inside the body of another while loop is known as Nested while loop in C++ programming language.
one iteration of the outer loop initially executed before the inner loop begins to execute. but the execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false).
The condition of the inner loop is satisfied flow of control come out to again outer loop for next iteration
Syntax
while(test_expression_1) { statement(s) while(test_expression_2) { statement(s) } }
#include <iostream> #include <conio.h> using namespace std; int main() { int i=0; while(i<=3){ cout<<"outer loop executes"<< endl; int j=0; while(j<=4){ cout<<"inner loop executes"; cout << "i = "<<i<<" and j="<<j << endl; j++; } i++; } getch(); return 0; }
When the above code is executed, it produces the following results:
Outer loop executes Inner loop executes i=0 and j=0 Inner loop executes i=0 and j=1 Inner loop executes i=0 and j=2 Inner loop executes i=0 and j=3 Inner loop executes i=0 and j=4 Outer loop executes Inner loop executes i=0 and j=0 Inner loop executes i=0 and j=1 Inner loop executes i=0 and j=2 Inner loop executes i=0 and j=3 Inner loop executes i=0 and j=4 Outer loop executes Inner loop executes i=0 and j=0 Inner loop executes i=0 and j=1 Inner loop executes i=0 and j=2 Inner loop executes i=0 and j=3 Inner loop executes i=0 and j=4 Outer loop executes Inner loop executes i=0 and j=0 Inner loop executes i=0 and j=1 Inner loop executes i=0 and j=2 Inner loop executes i=0 and j=3 Inner loop executes i=0 and j=4
Above the example initially, the outer loop is executed only once then inner loop executes until the test condition becomes false and same time display output.
The outer loop executes again one time and then continuously executes inner loop
This circulation is running until outer loop returns false
when the outer loop becomes the false flow of control skip the execution and jump out of the loop and end the loop execution
Program 1
#include <conio.h> using namespace std; int main() { int i=1; int j; while(i<=6) { j=1; while(j<=6){ cout <<j; j++; } cout<< endl; i++; } getch(); return 0; }
When the above code is executed, it produces the following results:
We can see three cases in this program
case 1
When we print j it produces the following results:
123456 123456 123456 123456 123456 123456
case 2
When we print i it produces the following results:
111111 222222 333333 444444 555555 666666
case 3
When we print “*” as a string it produces the following results:
****** ****** ****** ****** ****** ******
We can display elements from two dimension array in C++, using nested while loop
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { string arr[3][4]={ {"Niru","Saru","Pithu","Suthu"}, {"Kuru","Hari","Ram","Sam"}, {"Nilani","Roja","Mala","Mani"}, }; int i=0; while(i<=2){ int j=0; while(j<=3){ cout << arr[i][j] << endl; j++; } i++; } cout << "End the program" << endl; getch(); return 0; }
When the above code is executed, it produces the following results:
Niru Saru Piyhu Suthu Hari Ram Sam Nilani Roja Mala Mani End of the program
You can input element to the array and get output from an array using nested while loop
#include <iostream> #include <conio.h> using namespace std; int main() { int num[2][3]; cout <<"Enter element for array"<< endl; int i=0; while(i<=1){ int j=0; while(j<=2){ cout <<"num["<<i<<"]["<<j<<"]="; cin>>num[i][j]; j++; } i++; } cout << "\n your entered :" << endl; int k=0; while(k<=1){ int l=0; while(l<=2){ cout <<"num["<<k<<"]["<<l<<"]="<<num[k][l]<< endl; l++; } k++; } cout << "End of program" << endl; getch(); return 0; }
When the above code is executed, it produces the following results:
Enter element for array num[0][0]=35 num[0][1]=47 num[0][2]=58 num[1][0]=72 num[1][1]=60 num[1][2]=94 you entered num[0][0]=35 num[0][1]=47 num[0][2]=58 num[1][0]=72 num[1][1]=60 num[1][2]=94 End of program
While loop in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…