Get startedGet started for free

For loops

1. For loops

Let's talk about "for loops" now!

2. The 4 parts of C++ for loops

C++ for loops differ from R loops and are more general. A C++ for loop can be broken down into 4 parts.

3. For loops - the initialization

The initialization part contains code that is executed once, at the very beginning of the loop. Most of the time, it is used to initialize the iteration variable. Variables defined there have loop scope, i.e., they stay alive until the end of the loop and are not visible outside the loop.

4. For loops - the continue condition

The continue condition is evaluated at the beginning of each iteration. If the condition is met the loop continues.

5. For loops - the increment

The increment is executed at the end of each iteration. Most of the time, the increment is used to add 1 to an index variable.

6. For loops - the body

The body of the loop contains a set of statements executed at each iteration.

7. Typical for loop

The C++ for-loop is more general than the R for-loop. However most of the time, you will see and use this loop, as this is the most typical for loop in C++.

8. Typical for loop

In the initialization part of the loop, we create an int called "i" and set its value to zero. Remember that in C++, you need to specify the type for each variable so a simple "i" equal zero would not suffice. That variable "i" is our loop index, and is available in all parts of the loop.

9. Typical for loop

In the continue part, we test if that index is strictly lower than some value "n". The loop will stop the first time "i" becomes greater or equal to "n".

10. Typical for loop

In the increment, we use the special shortcut syntax "i++". "i++" is short for "i = i + 1". "i++' simply increments "i" by one. As opposed to R where we typically loop between 1 and n, idiomatic C++ loops go from 0 to "n -1". We will see this become relevant in the next chapter when we see that C++ vectors are indexed from 0.

11. Example: sum of n first integers

In this small example, we calculate the sum of the first n integers.

12. Breaking out of a for loop

The continue condition is typically in control of when the loop stops, however sometimes you want to exit a loop early, perhaps in the middle of its body because something unexpected happened. This is where the "break" keyword comes in. It allows us to exit the loop prematurely based on something that happens in the body. This can be useful when the condition to continue looping is more suitably evaluated in the middle of the loop body, or simply when you want to keep the condition as simple as possible.

13. Newton iterative method to calculate square roots

Let's have a look at an example. Newton's legacy is not limited to falling apples, we also owe him an iterative method to approximate the calculation of square roots. Obviously, in real life, you would use the "sqrt()" function in C++ just as you would in R, but let's forget about it for the next few exercises. Finding the square root of a positive number "S" is the same as finding the root of the equation x squared minus S. We can then apply Newton's root finding method, which leads to a nice iterative expression.

14. Newton's method in C++

For a first version, we can decide a large enough number of iterations, and an initial guess for the result, which we store in the variable "res". Then, as we want to perform the same operation n times, we use the classic form of the for-loop with the variable "i" ranging from 0 to "n - 1". The body of the for-loop simply consists of updating the result with the formula.

15. Let's practice!

Your turn to write a for-loop!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.