Unit 2: Making Choices
Ones and zeros, literally.
Unit 2 of 11 in C programming for kids. Its 4 lessons are One and Zero, If / Else, And, Or, Not and Bouncer Bot — 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 is free for ever, because the first two units of every track are. Try it in the app.
⚖️ One and Zero
C has no True or False
Python printed True. Plain C has no such word — a comparison gives you the number 1 for true and 0 for false. That is all "true" has ever meant to a computer.
C
printf("%d\n", 5 > 3);
printf("%d\n", 2 == 4);
It prints
1 0
Try it yourself
What does this print?
C
printf("%d\n", 10 > 20);
Which one asks "are these the same?"
- =
- ==
- >=
- !
Answer them in the app
🔀 If / Else
Braces, like JavaScript
Python used indenting. C uses curly braces { }, and the question goes in round brackets. The indenting is only there to help humans read it — C ignores it completely.
C
int age = 7;
if (age > 10) {
printf("Big kid\n");
} else {
printf("Little kid\n");
}
It prints
Little kid
Try it yourself
What does this print?
C
int coins = 12;
if (coins > 10) {
printf("Rich!\n");
} else {
printf("Keep saving\n");
}
This compiles, but it is a classic bug. What went wrong?
C
int lives = 3;
if (lives = 0) {
printf("Game over\n");
}
- Nothing — it prints Game over
- It uses = instead of ==, so it sets lives to 0 instead of asking
- if needs a semicolon
- lives cannot be 0
Answer them in the app
🔗 And, Or, Not
Asking two things at once
&& is true only when both sides are; || when at least one is. Remember C answers with 1 and 0.
C
printf("%d\n", 1 && 0);
printf("%d\n", 1 || 0);
printf("%d\n", !0);
It prints
0 1 1
Try it yourself
What does this print?
C
int age = 12;
printf("%d\n", age > 10 && age < 15);
What does this print?
C
int rain = 1;
int coat = 0;
printf("%d\n", rain && coat);
Answer them in the app
🏆 Bouncer Bot
Try it yourself
The bouncer lets in anyone 13 or older. What does this print for age 13?
C
int age = 13;
if (age >= 13) {
printf("Come in\n");
} else {
printf("Too young\n");
}
Which line would WRONGLY turn away a 13-year-old?
- if (age >= 13)
- if (age > 13)
- if (13 <= age)
- if (!(age < 13))
Answer them in the app