Intermediate 8: For-Loops
Another kind of loop used often by programmers is the for-loop



For-loops are another type of looping control structure used often in programming.  Unlike the while-loop, a for-loop is repeated a specific number of times.


For Loops

The conditional statement of a for-loop follows a very specific format.

for( ; ; ) {}

for(initialization ; condition ; increment) {

initialization – This is where you initialize and define the for-loop’s private variable

condition – The is the conditional statement for which the loop will repeat.   This must use the private variable.

increment – The private variable is increased or decreased by the increment after each loop.

statement – This is the section of code that is read for each loop.


Let’s start with an example

Example 1: Basic For-Loop



The for-loop conditional statement can be broken down into the 3 distinct sections.

The initialization states that our private variable i  will be an integer with an initial value of i = 0 .


The condition states that our loop will continue for as long as the condition i < 5  is true.

The increment uses the special sign i++ .  At the end of each read of our loop, the private variable i  will be incremented by + 1.


Putting this all together, the for loop says

With our variable i starting at zero,
repeat this section of code for as long as,
is less than 5.
After each repeat, add 1 to i.

i  starts at 0, and 1 is added at the end of each loop.  This means the loop will occur 5 times before i < 5  is no longer true.


i = 0
i = 1

i = 2
i = 3
i = 4
i = 5

The 6th loop will not execute.


Example 2: A While-loop as a For-loop

A for-loop can be thought of as a very specific while loop.  Here we will imitate the for-loop from example 1 using a while-loop.



Here you can see we get the exact same results.

Using the Private Variable

An obvious use of the for-loop is to repeat a statement a set amount of times.  For example, to make 5 beeps before beginning a program you could simply write


An equally exciting way to use the for-loop is utilizing the private variable within the statement.

private variable is a variable that can only be accessed within a very limited scope.  For a for-loop, the variable that is initialized within the condition is a private variable.


Example 3: Using the Private Variable



Here we print out the variable i .  Notice we can use the variable i just like any other variable within our for-loop.  It has the unique feature of changing each loop.

You cannot use the private variable outside of the for-loop


Example 4: Bouncing LEDs

We can use the incrementing variable to make our controller’s LED appear to be bouncing from side to side


Flying Circles with For-Loops

Programming the CoDrone to fly in a circle is the perfect way to show off our new for-loop skills.

Computers are naturally a bit bad at round edges, mainly because computers think in coordinates and lines.  We always have to move our CoDrone in either the Roll or Pitch direciton.

This makes it easy to program a square


But how would you program a Pitch and Roll circle?

The answer is that you don’t.  Instead of programming a circle, we’re going to program a shape that imitates a circle.

Take a square and add more sides to it

We now have an octagon, or an 8 sided shape.  Notice that we added diagonal sides to our square.  If we add even more sides


We slowly begin to see a circle taking shape.  It’s not a real circle, but if we add enough sides we can imitate a circle pretty well.


Using our for-loop, we are going to set the Pitch and Roll of each individual side



Example 5: Circle Function with For-Loops

This Circle()  function uses a for-loop to split the circle into 40 sides.

for the first 10 sides

i = 0, Pitch = 5, Roll = 50
i = 1. Pitch = 10, Roll = 45

i = 2, Pitch = 15, Roll = 40
i = 3, Pitch = 20, Roll = 35
i = 4, Pitch = 25, Roll = 30

ect.. until
i = 9, Pitch = 50, Roll = 0

At this point, we no longer want to add +5  to pitch, but instead want to begin subtracting -5 , so we include our next 10 steps in a separate if-statement.

The length of the delay()  at the end will determine how long each side of the circle is.  Increasing this will increase the diameter of the circle.



Using if-statements to Imitate for-loops

One potential downside of a for-loop is that it prevents your code from looping until the for-loop is completed.

In a way, it acts a lot like delay()  does.  The code does not move past the for-loop until all of the loops are completed.  For example, during the Circle()  function example above, you cannot use the kill-switch until the maneuver is finished.

One way around this is to create a ‘fake’ for-loop using if-statements.


Example 6: If-statement instead of for-loop

To replace this for-loop



we can use



You can see that both of these functions count upwards from 0 to 9, but the if-statement function allows void loop()  to continue looping.

using the prefix static before our integer declaration ensures that the value of our integer i is not overwritten every time the declaration is read.


Activity:  Hula Hoop


Write a function that performs an autonomous figure-8.