Friday, August 16, 2013

Nested loop in c++

What is nested loop in c++

There are 3 types of nested looping in c++ and these are, nested for loop, nested while and lastly the nested do.. while statement. Nested looping in c++ simply means, loop inside a loop or in other words loop statement within a loop statement. The main and subfolder structure is similar in the nested looping. See the figure bellow.

  • ABOUTMAIN FOLDER
    • C++SUB FOLDER
      • CPPSUB FOLDER
    • MakeSUB FOLDER
    • LearnSUB FOLDER
    • Turbo CSUB FOLDER

The color blue box represents as the main folder and it contains several folders inside, and as you noticed the c++ subfolder contain a subfolder inside, and this kind of structure is the best representation of a nested looping statement in cpp.

The nested looping can handle almost 256 level of nesting statement, how to count the level in nested looping? In the figure above the level one is the blue box entitled About, level 2 is the green boxes and the level 3 is the color red box entitled CPP. The second figure show, how the curly brace identified the looping within the main loop.

  • WHILE(CONDITION)MAIN or PARENT Loop
    • {OPEN Curly brace
      • WHILE(CONDITION)SUB or Child Loop
        • {OPEN Curly brace
          • CODE TO EXECUTESUB or Child Loop code to execute
        • }CLOSE Curly brace
      • CODE TO EXECUTEMAIN or PARENT Loop code to execute
    • }CLOSE Curly brace

Try to hover your mouse on the top of main while(condition), as you notice the color of paired curly braces become grey, it means that all codes or statement enclose by the paired of grey curly braces is instantly executing, it will not stop its execution as long the condition in the main while loop is true. The main while loop contain while loop inside and that is the reason why it become nested, simply loop within a loop. Now try to hover your mouse in the sub while(condition) as you notice the paired curly braces become grey, and codes that will executed is only the codes enclose by the grey curly braces, the code outside of the grey curly braces will not execute or triggered.

The different types of nested looping.

The nested for loop statement in c++ syntax:

for (initialization; condition; increment or decrement)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   for (initialization; condition; increment or decrement)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested while loop statement in c++ syntax:

while(condition)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested do while loop statement in c++ syntax:

do
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   do
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }while(condition);

}while(condition);
for (initialization; condition; increment or decrement){
   do
   {
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
   }while(condition);
}

I hope you learn some something in c++ nested looping statement, and I forgot to mention you can mix those three(3) types of nested loop (nested for loop, nested while loop, nested do while loop) to generate a mix nested loop (code above).

2 comments: