Today’s guide will be over Looping in C++. I have been wanting to get to my latest C++ article for a while. So lets break out our favorite text editor and compiler and have some real fun today! Looping is one of the basic concepts in programming and must be learned by every programmer. It is not very hard at all but I will include several examples. Other programming languages have the same basic concepts, there is just different syntax.
You can use any text editor you want. If you choose to go this route you will obviously have to use a separate compiler also. It depends on where I am on whether I use an editor or an IDE. If I use an editor I prefer the Atom editor or notepad++. There are other good ones so these are not the only two. If you want to use an IDE then I would recommend VS Code. These are all free to use.
Looping in C++
Looping in c++ will generally require some specific operators. The loops are used for mathematical expressions, testing, and reading from files. You use loops when you want to execute some code over and over. Creative uses abound, however, and you should indulge yourself whenever you think of a new use. So once you get the hang of things try and experiment some. Your usage and skills will grow the more you do that.
Types of Loops
There are a few different kinds of loops. Looping in C++ will require you to be proficient in all of them. Examples include while loops, the do-while loops and for loops. These loops are used in different scenarios. The one you choose will depend on your situation. When you get more experience it will be easier to recognize when to use each type of loop. Experience is gained through solving problems. We will be doing some of that after my examples.
Incrementing and Decrementing Values
The first step in learning how to use looping in c++ is to learn how to control your loop. When you are looping in c++ you will have to modify variables and their values often. Increasing a value is called incrementing. Decreasing a value is called decrementing. Both of these techniques allow you to set a condition for you loop to stop or start.
The increment symbol is [ ++ ]. The decrement symbol is [ — ]. They should not be too hard to memorize.
This is a short example of incrementing a variable.
As before we have our #include and namespace. If you have been following my posts and are trying to learn how to program then you should be familiar with this shell. Everything happens in the main function. I commented these few lines because I should and to make it easier to show what I am talking about.
int digit = 1; Our variable is [digit] and its an integer type. It is initialized to 1 because we need it to start there.
cout This is our output command which gives some text and then the current value of the [digit] variable.
digit++ The ++ increments the variable [digit].
cout This once again outputs our text and again gives the current value of [digit] which is now increased by 1.
This whole series of variables, incrementations, and outputting is very common in applications.
In order for a loop to start or terminate it will have to meet a certain condition. This will be a math or logic true/false. That means boolean logic here. It will depend on the need of your program and output.
Here is an example of a [While Loop]. It is the most basic loop but none are complex. As you can see I initialize my variable to 1, increment it, and then stop it after it reaches a value of 5. The loop runs until the condition is not met. An important fact to remember about [While Loops] is they will not always progress through the loop. It will test but it could just be false the first time through. When that happens this loop will just be skipped.
Do While Loops
This is the other version of the [While Loop]. It lets you do a process until a condition is met. It is a subtle difference and both of these loops can be used interchangeably a lot. A [Do While] loop will always do something at least once, which is different than the above kind of loop. It is useful when you need something to happen at least one time before testing further.
Here is the last of the basic loops. This one is entirely different but is quite useful. This loop structure is most useful when you know how many iterations you need until the loop needs to stop.
In this loop above you can see that I just say something simple for 20 times. After that the loop stops because the condition is not met. Everything else there should be easy to understand.
This can be a very useful thing to do and it is ideal for loops to handle. A running total is the sum of a group of numbers that accumulate every time the loop goes through its motions. This technique can tell you this sum of numbers and how many loops happened to get this sum. All useful information. The control variable is called the accumulator. This control variable is almost always initialized to zero at the very start.
Go ahead and type this out. You actually learn a little when you type programs out so I always recommend that.
Those are a few examples of looping in c++. As you can see it is easy to do but it is one of the foundations of programming. I will update this with more examples so it will be something you can check back on. Please share this article with your friends, they could find it interesting too!