Unit 3: Loops & Arrays
Doing things many times.
Unit 3 of 11 in C programming for kids. Its 5 lessons are Repeating, Keep Going Until, Arrays, Functions and Number Cruncher — below is everything each one explains, and a question or two from it to try.
Every sample on this page was compiled and run with gcc before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🔁 Repeating
Three jobs in one line
A C for loop packs three things into the brackets, separated by semicolons: where to start, when to keep going, and how to step.
C
for (int i = 0; i < 3; i++) {
printf("%d\n", i);
}
It prints
0 1 2
Try it yourself
How many lines does this print?
C
for (int i = 0; i < 5; i++) {
printf("hi\n");
}
- 4
- 5
- 6
- forever
What is the LAST number this prints?
C
for (int i = 1; i <= 4; i++) {
printf("%d\n", i);
}
Answer them in the app
♾️ Keep Going Until
When you do not know how many times
A for loop needs the count up front. A while loop just keeps going as long as its test stays true.
C
int lives = 3;
while (lives > 0) {
printf("%d\n", lives);
lives--;
}
It prints
3 2 1
Jumping out, and skipping ahead
break leaves the loop at once. continue skips the rest of this turn and starts the next.
C
for (int i = 0; i < 6; i++) {
if (i == 2) continue;
if (i == 4) break;
printf("%d", i);
}
printf("\n");
It prints
013
Try it yourself
What is the LAST number this prints?
C
int n = 1;
while (n < 20) {
printf("%d\n", n);
n = n * 2;
}
What goes wrong here?
C
int n = 5;
while (n > 0) {
printf("%d\n", n);
}
- Nothing, it prints 5 five times
- n never changes, so it prints forever
- while needs a semicolon
- It prints nothing
Answer them in the app
📋 Arrays
A row of boxes, fixed for life
An array is a row of boxes all of the same type. Unlike a Python list it can never grow or shrink — you decide the size when you make it, and that is that. Counting starts at 0.
C
int nums[3] = {4, 8, 15};
printf("%d\n", nums[0]);
It prints
4
Nobody is checking
This is the scary part of C. Ask for nums[9] in an array of 3 and C will not stop you — it hands back whatever junk happens to sit in that memory. Python would have raised an error. In C, staying inside the array is your job.
C
int nums[3] = {4, 8, 15};
printf("%d\n", nums[9]); // junk, or a crash
Try it yourself
What does nums[1] give?
C
int nums[3] = {4, 8, 15};
- 4
- 8
- 15
- 1
What does this print?
C
int nums[3] = {4, 8, 15};
printf("%d\n", nums[2]);
Answer them in the app
🧰 Functions
Say what goes in and what comes out
A C function must declare the type it returns and the type of everything handed in. int double_it(int n) means "give me an int, I will give you back an int".
C
int double_it(int n) {
return n * 2;
}
int main() {
printf("%d\n", double_it(5));
return 0;
}
It prints
10
Functions that hand nothing back
If a function only does something — like printing — and has nothing to return, its type is void.
C
void cheer() {
printf("Woohoo!\n");
}
Try it yourself
In int add(int a, int b), what does the first int mean?
- The function gives back an int
- The function takes one number
- The function is called int
- Nothing, it is decoration
What does this print?
C
int add(int a, int b) {
return a + b;
}
int main() {
printf("%d\n", add(3, 4));
return 0;
}
Answer them in the app
🏆 Number Cruncher
Try it yourself
What does this print?
C
for (int i = 0; i < 3; i++) {
printf("%d ", i * 5);
}
printf("\n");
What does this print?
C
int scores[4] = {10, 20, 30, 40};
printf("%d\n", scores[1] + scores[3]);
Answer them in the app