Home | | Computer Science 11th std | C++ Nesting of loops

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ Nesting of loops

A loop which contains another loop is called as a nested loop.

Nesting of loops

 

A loop which contains another loop is called as a nested loop.

The syntax is given below:

for (initialization(s); test-expression; update expression(s))

{

      for (initialization(s); test-expression; update expression(s)

      {

      statement(s);

      }

statement(s);

}


 

Illustration 10.17 C++ program to display matrix multiplication table using nested for loop

#include<iostream>

using namespace std;

int main(void)

{

      cout<< "A multiplication table:" <<endl <<" 1\t2\t3\t4\t5\t6\t7\t8\t9" <<endl<< "" <<endl;

      for(int c = 1; c < 10; c++)

      {

      cout<< c << "| ";

      for(int i = 1; i< 10; i++)

      {

      cout<<i * c << '\t';

      }

      cout<<endl;

      }

return 0;

}

Output

A multiplication table:    

1    2   3      4        5        6        7        8        9

1| 1     2      3        4        5        6        7        8        9

2| 2     4      6        8        10      12      14      16      18

3| 3     6      9        12      15      18      21      24      27

4| 4     8      12      16      20      24      28      32      36

5| 5     10    15      20      25      30      35      40      45

6| 6     12    18      24      30      36      42      48      54

7| 7     14    21      28      35      42      49      56      63

8| 8     16    24      32      40      48      56      64      72

9| 9     18    27      36      45      54      63      72      81

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ Nesting of loops |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.