Now that we have had a chance to take a look at some of the arrays, we need to look at how we can create loops in C++.
These loops are going to be helpful because they save a lot of time when we want to write out our codes.
If you need to get your code to repeat a single part a number of times, whether it is just a few or a large amount, then you do not want to waste all of your time writing out that many lines of code unless you need to.
This can take up a lot of time, gets tedious, and can make the code harder to read through.
With a loop, you will be able to take some of the code that you already wrote out, and then, after writing it out just one time, you will be able to set it up to repeat itself a number of times.
The number of times the loop will repeat is going to be based on how long it takes until the condition is met.
Make sure that when you write out the loop you actually add in the condition, though.
This is easy to forget when we are writing codes, and then the loop gets stuck and will not be able to stop it unless you end the program.
There are actually a few types of loops that we can work with, and it depends on what you would like the code to do in concerns with them.
The main types of loops that we are able to work with include:
The While Loop
The first loop we can look at here is known as the while loop.
The while loop is the choice that is going to continue on through our loop and will repeat the necessary lines, as long as the conditions you set are met.
The program is going to read on through the information and then test for that condition each time that it does the loop.
Then the loop will restart again if it finds that the conditions were met.
Once it gets to the point where the condition is no longer true, the loop is going to stop and will head on to the next part of the code if there is more to your program.
With that in mind, the while loop is going to be pretty simple to work with.
To make a simple while loop, you really just need a few lines of code to get it done.
The syntax that we are able to utilize in order to work on the while loop is found below:
While(condition)
{
Statement(s):
}
The For Loop
Now it is time to move on to the second type of loop that we can work with is known as the for a loop.
This for loop is the one that only repeats itself after first testing that the conditions are true.
With the while loop that we did above, the loop will go through, and then test the conditions.
With the loop, the conditions are tested first, and then the loop will proceed.
This means that based on the input that is given to the computer or the program, it is possible that the program will test the condition, it will be seen as false, and the for loop will not happen even once inside of the program.
If you are doing a program where the loop has to happen a minimum of one time, then the while loop is the right one to choose.
If it isn’t too important, or the code will be fine in the case of the loop not going if the conditions are not met, then the for loop can be a good one.
If you are working on your program and you determine that the for loop is the right one for you, then you will need to go through and write out some code to do this.
It is similar to the while loop that we did above, which is going to make it easier to handle overall.
The code that you need to use to make the for loop behave in the right manner includes:
For (iit: conditions: increment)
{
Statement(s);
}