C++ While Loops With Example Code Break Down
This tutorial will focus on how to use the while loop in C++ and will include example code to aid in explanation. While loops are what is known as a “control flow statement“. This basically means the code inside of the loop will be executed over and over again until the set condition becomes false. The while loop is similar to a for loop but is more simple because there is only a single conditional statement. If you are interested in learning about for loops check out this guide on that instead. Also, if you don’t have a C++ compiler DevC++ is a fantastic lightweight portable compiler.
The Basics of How The While Loop Works in C++
As mentioned earlier, the while loop works off of a statement called a “conditional statement”. Basically, a while loop will run code until the conditional statement returns false. At that point the program will move on to the next statement in your program. A while loop with conditional statement will take the form of “while(CS){code to loop}” where CS is any conditional statement you might input and code to loop is the code that will be looped until the conditional statement returns false.
A Basic Example of How To Use a While Loop
A basic example of a while loop with output in C++ is shown below. Feel free to copy and paste the code into your own IDE and play around with it. After all, tinkering with a programming language is one of the best ways to learn it!
#include <iostream> //while loop example code 1 int x = 0; int main(){ while(x <= 5){ x++; std::cout << "loop run " << x << std::endl; } return 0; }
When this program starts it will check to see that “x <= 5“. Since we set “x = 0” originally this will initially be true thus the while loop will make a loop and run through the “x++” on line 6 which will add an integer value of 1 to x making it become “0 + 1” or 1. This runs continuously resulting in the value of x increasing until it reaches a value of “x = 6“. At that point the condition of “x <=5” is no longer True so the program exits the loop and continues on to other statements in the program.
C++ Infinite While Loop Example Code
When learning how to use the while loop in C++ it is extremely important to pay attention to your conditional statement. If there is no way for your conditional statement to become false you will create what is known as an “infinite loop”. When this happens, your while loop won’t stop without manual action. An example of an infinite while loop and output can be seen below.
#include <iostream> //infinite C++ while loop example int x = 0; int main(){ while(x <= 5){ //x++; std::cout << "loop run " << x << std::endl; } return 0; }
As seen in the above example code the “x++” has been commented out which results in the conditional statement “x <= 5” never becoming false. Since “x” was originally set to 0 and nothing within the while loop changes the value of x, the loop outputs the value of 0 infinitely. Obviously infinite loops are unwanted but as long as your conditional statement can become false you don’t have to worry about it too much.
A Slightly More Complex Example for Using C++ While Loops
To round this tutorial off lets look at a more complex example of while loops in C++. When using while loops you may sometimes want to use more than one loop within the same loop (sounds confusing right?). Doing this is called “nesting”. When done this way, the program runs from top down as usual but stays within the innermost while loop until the condition for that loop has been met. It will then work its way back out to the outermost loop. An example of this type of program is shown below.
#include <iostream> //while loop complex example code bool inside_loop1 = false; bool inside_loop2 = false; bool inside_loop3 = false; bool tag; int main(){ while(!inside_loop1){ if(!tag){ std::cout << "This will run first." << std::endl; } if(inside_loop2){ std::cout << "Now the program has worked its way all the way back to the first loop" << std::endl; inside_loop1 = true; } while(!inside_loop2){ if(!tag){ std::cout << "Then this will run second." << std::endl; } if(inside_loop3){ std::cout << "At this point the program has worked its way back out to the second while loop." << std::endl; inside_loop2 = true; } while(!inside_loop3){ std::cout << "Then this will run third." << std::endl; std::cout << "Then the program will work its way back out." << std::endl; tag = true; inside_loop3 = true; } } } std::cout << "Now we have exited all of the loops! Horay!!!!" << std::endl; return 0; }
The Code Break Down
While Loop 1
For the last part of this C++ while loop tutorial lets try to understand how the code works. The above input and output code basically represents how while loops operate. When the program begins to run it will check to see that “inside_loop1” is False. When it sees that it is it will then progress to see if “tag” is False. In this code, “tag” refers to the bool variable that is seen in the 3rd while loop. Basically when the program reaches the innermost while loop the “tag” variable will be set to true and this tells the rest of the program that the innermost while loop has already been reached and that we don’t want it to output the same message again.
While Loop 2
The program then progresses to the second if statement within the first while loop and check to see if loop 2 has been reached. That is, if “inside_loop2” has been set to True yet. When it sees that it hasn’t it will progress further in to the second while loop. It will react the same way with the “tag” variable since the third while loop hasn’t been reached yet. It will also check if loop 3 has been reached or not with the second if statement. That is, if “inside_loop3” has been set to True.
While Loop 3
When is sees that it hasn’t it will progress to the innermost while loop, output the text, then set both “tag” and “inside_loop3” to true. Once it has done this it will work its way back to the second while loop, see that “tag” is now True (so won’t run that code within the if loop) then see that “inside_loop3” is now True and will run the code within that if statement. The same thing will then happen when the code works its way back to the first while loop and the final text within the while loop “Now the program has worked its way all the way back to the first loop” will output. After that the “Now we have exited all of the loops! Horay!!!!” message will appear which lets us know we have successfully made it out of all those while loops!
Final Thoughts
Learning to use while loops in C++ is extremely beneficial. Hopefully this tutorial on C++ while loops has helped you better understand the statement! The biggest tip is to not give up! learning to program isn’t easy so you have to keep at it! Make sure to play around with programming and try different things to get a good sense of how everything works. Be sure to comment below if you need any additional help with how to use while loops in C++!
Leave A Comment
You must be logged in to post a comment.