In this tutorial, We will learn about Nested for loop in CPP programming language
If the for loop contains another for loop inside its body, it is known as nested for loop in C++ language. Every time, both the outer loop and inner loop should complete their circulation.
Syntax
for(initialization; test_expression; updating_statement){ //codes inside the body of outer loop for(initialization; test_expression; updating_statement){ //codes inside the body of outer loop } }
Here, there is two for loops, Outer for loop exists inside a for loop known as inner for loop
Nested for loop has two types of loop
Outer for loop executes only once. when its test expression is true. the flow of control moves to inner loop iteration and inner loop executes until its test expression is false.
When the test expression of the inner loop is false, the flow of control skips the execution of the inner loop and come out to execute the outer loop again.
When the test expression of the outer loop is false, the flow of control exit from the loop control and go to rest.
Example
#include <iostream> #include <conio.h> using namespace std; int main() { for(int i=1; i<=3; i++){ cout << i<<"outer loop executed only once\n" << endl; for(int j=1; j<=4; j++){ cout << j<<"inner loop executed until to complition" << endl; } cout<<"\n"; } getch(); return 0; }
When the above code is executed, it produces the following results:
1 outer loop executed only once 1 inner loop executed until to complition 2 inner loop executed until to complition 3 inner loop executed until to complition 4 inner loop executed until to complition 2 outer loop executed only once 1 inner loop executed until to complition 2 inner loop executed until to complition 3 inner loop executed until to complition 4 inner loop executed until to complition 3 outer loop executed only once 1 inner loop executed until to complition 2 inner loop executed until to complition 3 inner loop executed until to complition 4 inner loop executed until to complition 4 outer loop executed only once 1 inner loop executed until to complition 2 inner loop executed until to complition 3 inner loop executed until to complition 4 inner loop executed until to complition
Example
program 1
#include <iostream> #include <conio.h> using namespace std; int main() { for(int i=1; i<=10; i++){ for(int j=1; j<=10; j++){ cout <<j; } cout << "\n"; } getch(); return 0; }
When the above code is executed, it produces the following results:
12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
In this program, initially outer for loop start its execution and executes only once. When the test condition is evaluated to be true, the flow of control enters the inner for loop.
Then, the inner for loop completes its execution unless the test condition evaluated to be false.
In that case, the flow of control come out to the outer loop again and executes only once.
Then the flow of control enters the inner loop again.
This circulation should be held until outer loop test expression is false.
When the outer loop test expression is evaluated to be false, the flow of control comes out of the outer loop body and the loop control goes to rest.
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { for(int i=1; i<=10; i++){ for(int j=1; j<=10; j++){ cout <<"*"; } cout << "\n"; } getch(); return 0; }
When the above code is executed, it produces the following results:
********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Program 3
We can use nested for loop to display in two dimension array and the three-dimensional array of elements in C++ language
#include <iostream> #include <conio.h> using namespace std; int main() { int marks[3][4]; cout<<"Enter the marks for Array \n"<<endl; for(int i=0; i<=2; i++){ for(int j=0; j<=3; j++){ cout<<"marks["<<i<<"]["<<j<<"]"<<":"; cin>>marks[i][j]; }//input marks from user } cout<<"\nYou entered:"<<endl; for(int i=0; i<=2; i++){ for(int j=0; j<=3; j++){ cout<<"marks["<<i<<"]["<<j<<"]" <<":"<<marks[i][j]<<endl; }//output marks } getch(); return 0; }
When the above code is executed, it produces the following results:
Enter the marks for Array marks[0][0]:56 marks[0][1]:76 marks[0][2]:78 marks[0][3]:87 marks[1][0]:89 marks[1][1]:98 marks[1][2]:90 marks[1][3]:54 marks[2][0]:45 marks[2][1]:65 marks[2][2]:56 marks[2][3]:73 you entered marks[0][0]:56 marks[0][1]:76 marks[0][2]:78 marks[0][3]:87 marks[1][0]:89 marks[1][1]:98 marks[1][2]:90 marks[1][3]:54 marks[2][0]:45 marks[2][1]:65 marks[2][2]:56 marks[2][3]:73
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { string Name[2][2][3]; //Array diclaration cout<<"Enter your name here\n"<< endl; for(int i=0; i<=1; i++){ for(int j=0; j<=1; j++){ for(int k=0; k<=2; k++){ cout <<"Name["<<i<<"]["<<j<<"]["<<k<<"] :"; cin>>Name[i][j][k];//Take input from user } } } cout<<"\nyou Entered :"<< endl; for(int i=0; i<=1; i++){ for(int j=0; j<=1; j++){ for(int k=0; k<=2; k++){ cout <<"Name["<<i<<"]["<<j<<"]["<<k<<"] :"<<Name[i][j][k] << endl;//Display names from array } } } getch(); return 0; }
When the above code is executed, it produces the following results:
Enter your name here Name[0][0][0] :Jhon Name[0][0][1] :Smith Name[0][0][2] :Nilu Name[0][1][0] :Suman Name[0][1][1] :Shagan Name[0][1][2] :Mackal Name[1][0][0] :Amir Name[1][0][1] :Khan Name[1][0][2] :Bala Name[1][1][0] :Robert Name[1][1][1] :Kamith Name[1][1][2] :Siya you entered : Name[0][0][0] :Jhon Name[0][0][1] :Smith Name[0][0][2] :Nilu Name[0][1][0] :Suman Name[0][1][1] :Shagan Name[0][1][2] :Mackal Name[1][0][0] :Amir Name[1][0][1] :Khan Name[1][0][2] :Bala Name[1][1][0] :Robert Name[1][1][1] :Kamith Name[1][1][2] :Siya
In the above program, initially, a user can enter the String value to the array for the store using nested for loop.
after we can display elements from the array using nested for loop.
For loop in Java language
For loop in C language
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…
View Comments
Thanks from your about programming tutorial