Introduction
The last lesson ended with six almost identical lines of Serial.println. A for loop is the cure.
for (int i = 0; i < 6; i++) {
Serial.println(notes[i]);
}
Three parts, separated by semicolons, all in the brackets:
int i = 0— where to start. Runs once.i < 6— carry on while this is true. Checked before every pass.i++— what to do at the end of each pass.
Together they say: start at zero, keep going while under six, add one each time. The block runs six times, with i being 0, 1, 2, 3, 4 and 5 — precisely the valid indexes of an array of six.
That is not a coincidence. for (int i = 0; i < length; i++) is the shape you will write for the rest of your life, because it lines up exactly with how arrays are numbered. Learn it as one unit, not as three separate decisions.
What you will be able to do
By the end of this lesson you can:
- Write a
forloop with its three parts in the right order. - Walk an array with a loop instead of repeating yourself.
- Count downwards, and in steps larger than one.
- Say why
<=in the condition usually reads one element too far. - Nest one loop inside another.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
→ The for Loop — the same idea driving real LEDs.
The three parts
for (int i = 0; i < 5; i++) {
Serial.println(i);
}
Prints 0, 1, 2, 3, 4.
| Part | When it runs |
|---|---|
int i = 0 | once, before anything |
i < 5 | before every pass — false ends the loop |
i++ | after every pass |
The order is: check, run, step, check, run, step… The condition is tested before the first pass, so a loop whose condition starts false never runs at all.
Walking an array
int notes[6] = {262, 294, 330, 349, 392, 440};
int length = sizeof(notes) / sizeof(notes[0]);
for (int i = 0; i < length; i++) {
Serial.print("notes[");
Serial.print(i);
Serial.print("] = ");
Serial.println(notes[i]);
}
Six lines of output from four lines of code — and adding a seventh note changes nothing here at all, because length works itself out.
< or <=
Challenges
Challenge 1
Replace six lines with four.
Take your array from ARD-PG-12 and print all six notes with a single for loop.
Then, without touching the loop, add two more notes to the array — 494 and 523 — and make the size [8].
If your loop used sizeof for the length, it already prints eight. If it used a hard-coded 6, fix it so it does.
That is the whole point of the exercise: which version needed changing in two places?
Log in to ask for the answer.
Challenge 2
Count in every direction.
Write four separate loops that print:
1to1010down to1- every even number from
0to20 1, 2, 4, 8, 16, 32, 64— doubling each time
Then write a fifth that prints the numbers 1 to 10 and their squares:
1 squared is 1
2 squared is 4
Log in to ask for the answer.
Challenge 3
A times table.
Print the 1 to 5 times tables as a grid, using two nested loops:
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
Getting the columns to line up is most of the work. Serial.print("\t") prints a tab, which does the job.
Then answer: how many times does the innermost Serial.print run in total?
Log in to ask for the answer.
Extra challenge
Statistics on an array.
Start with:
int readings[10] = {23, 45, 12, 67, 34, 89, 21, 56, 78, 43};
With loops only — no writing values out by hand — find and print:
- the total
- the average, to one decimal place
- the largest value
- the smallest value
- how many are above the average
The last one needs two passes: you cannot know the average until you have seen every value, and you cannot count what beats it until you know it.
Think about it: your sketch reads the array several times over. For ten values that is nothing. What if it were ten thousand readings from a sensor, arriving one a second — could you still find the average this way, and what would you have to keep instead?
Log in to ask for the answer.