Menu
Topics Index
...
`


Control Statements > Iteration statements (Loops) >
Siva Nookala - 11 Apr 2016
Nested loops are very common in programming, when we have to call a loop multiple times. For e.g., printing 1 2 3 4 5, can be done using a loop, but for printing it four times will need a loop inside a loop, also called as nested loops.
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

  • When working with nested loops, the outer loop changes only after the inner loop is completely finished (or is interrupted.).
  • There is no limit on the number of levels of nesting. for e.g., we can have one loop inside one another loop which is inturn is inside one more loop

    while(condition1)
    {
        while(condition2)
        {
            while(condition3)
            {
                ......
            }
        }
    }
  • There could be multiple loops at the same level inside one loop.

    while(condition1)
    {
        while(condition2)
        {
        }

        while(condition3)
        {
        }

        while(condition4)
        {
        }
        ...
    }
  • We can have combination of for and while loops in any order - for inside while, while inside for, for and while in the same level etc.

    while(condition1)
    {
        for(int i = 0; i < 5; i++)
        {
            while(condition2)
            {

            }

            for(int i = 0; i < 5; i++)
            {
            }
        }

        while(condition3)
        {
        }

    }

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.