How To Nest While Loops in C++ With Example Code


This guide will cover how to nest while loops in C++. Nesting can be a very useful tool when writing loop statements into your code. If you are unfamiliar with what nesting is, it is basically putting loop into another loop. For a refresher check out this guide on how to use while loops. What happens is the program will start at the outermost code then work its way to the innermost code. Once that happens it will make its way back out to the outermost code. The basic idea behind nesting for while loops is to have your outermost conditional statement become false only once your program has reached the innermost statement. In this way, your program will run from the top down then, in a way, it will run from the bottom back up as the conditional statements become false. 

Some Examples of Nested While Loops Within While Loops In C++


Example Code 1

Lets Look at a few examples of C++ nested while loops. In this first example we have a while loop nested within another while loop. Since the initial condition “reached_innermost_loop” is initially false it will display the first output to the console. Then it will work its way into the innermost while loop and output the messages there before setting the “reached_innermost_loop” boolean variable to true. Once this happens it works its way back out of both while loops and jumps to the next statement in the program. The code itself and the output from the code are shown below.

#include <iostream>
bool reached_innermost_loop = false;
int main(){
while(!reached_innermost_loop){
   std::cout << "we haven't reached the innermost loop yet this is the outermost loop!" << std::endl;
       while(!reached_innermost_loop){
       std::cout << "Now we are in the innermost loop!" << std::endl;
       std::cout << "Now we will work our way back out..." << std::endl;
       reached_innermost_loop = true;
                }
        }
std::cout << "We have now exited the while loops and jumped to the next statement since we reached the innermost loop." << std::endl;
return 0;
}

while loop nesting example output 1

Example Code 2

This Example will show that loops within loops can be set to run a number of times each time the outermost loop runs. What happens here is the first while loop starts and the “loop1passes” variable value increases by 1. The nested while loop then runs until it reaches 3 passes. At that point the program will exit that loop and jump back to the first loop. The first loop will set “loop2passes” back to 0 which causes the nested loop to repeat each time the outer loop repeats until the outer loop conditional statement returns false. 

#include <iostream>
int loop1passes, loop2passes, loop2passestotal = 0;
 int main(){
     while(loop1passes <= 5){
         loop2passes = 0;
        loop1passes++;
         std::cout << "Loop 1 has run " << loop1passes << " time(s) now" << std::endl;

         while(loop2passes <= 2){
             loop2passes++;
             loop2passestotal++;
             std::cout << "Loop 2 has run " << loop2passestotal << " time(s) now within loop 1" << std::endl;
         }
     }
return 0;
 }

nested while loop console output example 2

 

Example Code 3

With this third example let’s look at a little more complex nesting of some while loops. In this example we’ve got a few while loops. The second while loop is nested within the first while loop and the third within the second. Each time the program enters a loop its respective boolean variable “loop#tagged” (# is the loop number) is set to true. The code then progresses inward to the next while loop where the same thing happens. This results in code that runs to each nested loop, sets that that loop has been reached, then goes to the next while loop and so forth. This repeats until all loop’s conditional statement is no longer true. This code can also be used with integer variables to define a set number of passes of the loops. The code itself and the output from running it are shown below. It is recommended you mess around with it in your own IDE until you understand what is going on!

#include <iostream>
bool exitloops, loop1tagged, exitloop2, loop2tagged, exitloop3, loop3tagged = false;
int main(){
    while(!exitloops){
        if(loop1tagged){
            std::cout << "All loops have been tagged now so we will go on to the next statement in out program..." << std::endl;
            exitloops = true;
        }
        if(!loop1tagged){
        loop1tagged = true;
        std::cout << "Now we've tagged loop 1..." << std::endl;
                        }
            while(!exitloop2){
                if(loop2tagged){
                    std::cout << "Loop 2 has been tagged so we will exit loop 2..." << std::endl;
                    exitloop2 = true;
                }
                if(!loop2tagged){
                loop2tagged = true;
                std::cout << "Now we've tagged loop 2..." << std::endl;
                                }

                    while(!exitloop3){
                        if(loop3tagged){
                            std::cout << "Loop 3 has been tagged we will exit loop 3..." << std::endl;
                            exitloop3 = true;
                        }
                            if(!loop3tagged){
                            loop3tagged = true;
                            std::cout << "Now we've tagged loop 3..." << std::endl;
                                            }
                    }
            }
    }
std::cout << "We have exited the while loops and jumped to the next statement in our program..." << std::endl;
return 0;    
}

how to nest while loops with example code 2

Summary on Nesting C++ While Loops


In the first example we looked at a basic nested while loop program in C++ and how it works. The second and third examples went deeper into the concept. They showed how nesting can be useful to define a certain number of passes of loops within loops! Hopefully you now understand the basics of nesting while loops and know enough to mess around with them on your own. For any additional help on this topic feel free to leave a comment below!