while
loop and the for
loop.while
keyword, followed by a condition in brackets (you already know how to formulate these - they are exactly as found in if statements!), followed by the code you want to execute in curly brackets. Remember that we can also use a bool value as the condition if we want -- as in if statements, it's all about whether true
or false
is returned.number = number + 1
, number += 1;
, and some other we'll cover in the future).bool
s) serve a perfect purpose for this, and so we could do something like this:while(true)
(an infinite loop) and then using break;
to break out of the loop if the input is valid.number = number + 1
to add one to the variable number
that we were using at the time, to step through the while loop a certain number of times. We glossed over the fact that this was called incrementing and that there are a variety of ways to do it. The first I've just covered, the second is using the +=
operator to do something like number += 1
, but there is also a third, even easier way to accomplish this. This way, unlike the others, can only be used to add one to a variable rather than adding a number of your choice. This method is using something called the increment operator. The increment operator is shown in C++ by writing ++
on either the left or right side of a variable - as you might expect, it increments that variable.cout
! It's important to note that the operator does different things when put at different sides of a variable - this can be seen most noticeably when using the variable while incrementing as previously described. So firstly let's use the post-increment (the one with the ++
after the variable name), this should use the variable and then increment it. For example the following will output 10, then 11:++
before the variable name) will increment the variable before using it. So the following should output 11, then 11:--
and just like the increment operator has pre and post versions (which act just like the increment ones). I'm not really going to go into much depth here, as it should be blindingly obvious how to use the decrement operator if you know how to use the increment operator correctly. Take the following pieces of code for example, try and guess what will output what:while
. It, however, makes creating a loop which should loop a certain number of times really simple.main
function - I'm going to use an array of string
s (make sure to #include <string>
!):for
keyword, and then specifying three different sections, separated by semicolons, inside brackets. Code to execute each loop is then put inside curly brackets that follow this. The first section that the loop takes is called the declaration, and should contain the declaration (and probably initialization) of an iterator variable that the loop will use - just like the int number;
variable we used with some 'while' loops. The second section is called the condition - this should contain the condition which must remain true for the loop to keep going. The third and final section, is called the update - this should contain some code which updates the iterator variable (for example, incrementation). The general idea should look something like the following:i
going from 0 to 9):for
for example, you could leave all the sections empty and simply write:break;
could be used to break out of the loop at any point inside of it. Getting back to our purpose of looping through an array though, we want to execute the code inside as many times as our array has elements. We could hard-wire it in and have it loop round exactly five times and access element 'i' each stage of the loop, but this isn't the most elegant solution as we may wish to change the number of elements in the array at a later date, at which point we would have to update all the code (which isn't what we want). A better solution might be to get the exact number of elements in the array, but unfortunately there isn't a nice function or anything to do that -- we can however count the number of elements if we get a bit creative.sizeof
. With all of this in mind, the calculation to get the number of elements should look like the following:sizeof
the first element in the array above, the result would be the same if we divided by sizeof(string)
-- using the first element in the array is just slightly more flexible as we may wish to change the array datatype in the future (although hopefully not to anything too complex as this method of counting the elements doesn't work with everything).So now we can count the elements in the array, writing out the for loop shouldn't be too difficult. We want to create an iterator variable, which we'll call i
, loop while it is less that the number of elements (the calculation we just worked out), and then increment i
each time. The result should look something like the following:cout
that outputs the array element with the index of i
, i.e. all of the array elements from index 0 to index 9 - and we've finished our program! When outputting many numbers it can get confusing for the user, so it might also be a nice idea for the application to number each score it outputs -- what better way to do this than make use of the index number! We've covered everything necessary to create this program - if you feel like a challenge, try to create it all by yourself, if not, feel free to take a look at the code I whipped up below (with a little bit of extra cout
polish).