Tutorials Navigation
C++ Beginners Tutorial: Loops.
Tutorial Name: C++ Beginners Tutorial: Loops.
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 0
Views: 3,013
Related Forum: PC Building Forum
Share:
C++ Beginners Tutorial: Loops.
We can now write code that can read input from the user, do different things depending on that input, and then output the results, but what if we want to do the same action, but with slightly different parameters, several times over? It would be boring just to write the same thing out over and over again, and downright awkward if we didnt know how many times we needed to run that piece of code when we were writing it, say if it was dependent on something the user entered.
To do this, we can use something called a loop. A loop is a piece of code that is repeated several times, one after another until a certain condition is met. C++ has three types of loop; the while loop, the for loop and the do while loop. We shall start with the while loop, as its the simplest.
A while loop looks a lot like an if statement:
int userInput = 0;
while(userInput != 10)
{
cin >> userInput;
}
The use of the word while becomes evident when you understand what a while loop actually does. Here it is saying While the value in the userInput variable is not equal to 10, retrieve some input from the user. See, I told you that it was self-explanatory. As an aside, in C++ the ! sign means not, so != means not equal. Make sense?
Of course, if userInput is already 10 in the first place, then no code will be executed at all, and the loop will be skipped. In essence, a while loop can just be seen as being an if statement that jumps back to itself once the code inside it has been run.
Before we continue, it is important to note that in C++ the following is a legal piece of code:
while(1 == 1)
{
cout << "a";
}
In fact, it has its own special name; an infinite loop, as it, well, loops infinitely. Tracing through the code, you would get something like Is 1 equal to 1? Yes it is, print an a. Is 1 still equal to 1? Yes it is, print another a. Is 1 still 1? Yes, why wouldnt something be the same as itself? Print an a. and so on. This isnt the only way of making an infinite loop, there are others.
The second easiest type of loop, as its very similar to the while loop, is the do while loop. A do while loop looks like this:
int userInput = 0;
do
{
cin >> userInput;
} while (userInput != 10);
Whilst it looks quite different to the standard while loop you have just seen, is is essentially the same thing, except there is one important difference; whilst a while loop evaluates a conditional to see if it is true and then runs the code if it is, a do while loop executes some code and then checks to see if the conditional is true. Because of this, unlike in the first example, no matter what we initialised userInput to the code between the curly-brackets would be guaranteed to run.
The third type of loop C++ implements is the for loop. This is the most complicated of the three, but is powerful once you get your head around it. For now, we will just cover the simple uses of these loops. One such for loop can be seen below:
int i = 0;
for(i = 2; i < 10; ++i)
{
cout << i << "\n";
}
This piece of code is saying Start by setting the value of i to 2, then as long as i is less than 10, output its value to the screen and increment i. It should be clear that this for loop could be easily replicated using a while loop, however that uses several more lines of code and is generally much more untidy.
It is important to note each of the variables used in the first line of the for loop must be the same variable; they cannot be different ones. It is also important to remember that the first statement is, unlike a while loop, not a conditional, it is an assignment!
This kind of loop is especially useful for stepping through various pieces of data, as we will see later when we introduce arrays. For now, probably the most useful thing it can do is output a sequence of numbers as shown above. Thats still useful, mind.
Ratings
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,611)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C++ Beginners Tutorial: Loops." :: Login/Create an Account :: 0 comments