Earlier we looked at arrays, but they probably seemed like a convenience more than anything particularly useful, as all we’d done was replace writing a, b, c with a[0], a[1], a[2], which is more work! By using loops we can make them much more useful.

First we’ve defined a small array of different values which we would like to output to the screen. Instead of repeating code and outputting each one individually, we have used something called a for loop to iterate over every element in the array and perform the same output statement on tha element.

A for loop requires a loop index variable—conventionally called i, j, or k—which is first set to a starting value. Then so long as the expression following it (i < 5) is true, the code inside the {} will be run and at the end of that code the value of i will be modified according to the last statement (++i) in the for loop. Commonly the loop index will be an int, but there’s no reason for this to be the case.

Hence the body of this loop will be run with i = 0, then with i = 1, i = 2, i = 3, and i = 4, but not i = 5 as 5 < 5 is false. Whilst a lot of loops will involve setting the index to a starting value, checking it’s less than something and then incrementing, for loops are far more powerful than that.

Sometimes we want to stop a loop even if its condition is still true, such as if we’re searching for a certain value in an array; we want the loop to continue to the end, but there’s no point searching for longer if we’ve found the element we’re looking for. We can do this using the break statement, which we’ve already seen when dealing with switch.

Skipping certain iterations of a loop is also useful, such as if we want to skip all even values in a loop.

Of course this is a very simple example and the keen-eyed among will probably have noticed a considerable optimisation that removes the need for the continue in this case.

### Variable Scope

Note that we’ve moved the loop index into the for loop itself. This is a handy shorthand, but it comes with a caveat to do with variable scope. Every variable in C++ has a certain scope, which is the section of code in which the variable is declared. If you declare a variable inside a code block {} then it will only be available inside that block.

In the section on Conditionals you may have tried declaring a variable inside an if statement for example, but if you try and access it outside of the if you will get a compilation error.

When compiling with Clang I get

You can however redeclare variables in a different scope to one that they’ve already been declared in, even if the scopes are nested. Whichever variable is in the closest scope to the statement using it will be affected. What you can’t do though is declare the same variable twice within the same scope.

As you can see it can get a little complicated! The safest thing to do is just not reuse variable names unless they haven’t been used already in the scope.We’ll be going back to scope later when we cover functions and objects, but this is all that’s crucial for now.

### while Loops

The for loop isn’t the only looping construct in C++, though it is the most powerful. Using it we could create a loop that ran over and over while a given condition was true.

This is quite cumbersome and ugly though, so C++ provides a shorthand called the while loop.

Like the for loop equivalent, a while loop will not even run once if its condition is false from the start. (Hence we initialise userInput to 1, not 0.) In some circumstances though you do want your code to run at least once; enter the do while loop.

As the name and syntax implies we do the code in the {} while the condition is true. Just take note that the code block is in a different scope to the condition, so whilst this may look sensible it won’t compile!

### Exercise

Improve the prime number checker from the last exercise to allow for arbitrary size integers (up to long long) instead of just those less than 10. You don’t have to accept two numbers, just one will do this time.

There are a multitude of different algorithms and optimisations to check for primality, and finding primes is an active topic of research, but a simple algorithm is just to find all the factors of a number.

Solution