Unit 8: More Ways to Choose
switch, do/while and bits.
Unit 8 of 11 in C programming for kids. Its 4 lessons are Switch, Do It At Least Once, Working With Bits and Choice Master — 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.
🎛️ Switch
A tidier way to check one value
When you are checking the same variable against many values, switch reads better than a stack of else if. Each case is one possibility, and break says "stop here".
C
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Some other day\n");
}
return 0;
}
It prints
Wednesday
default catches everything else
default runs when no case matched. Leave it out and a value you did not expect quietly does nothing at all.
C
#include <stdio.h>
int main() {
char grade = 'C';
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
default:
printf("Keep going\n");
}
return 0;
}
It prints
Keep going
Forgetting break is the classic bug
Without a break, C carries straight on into the next case. Here 1 prints three lines, because nothing stops it.
C
#include <stdio.h>
int main() {
int n = 1;
switch (n) {
case 1:
printf("one\n");
case 2:
printf("two\n");
case 3:
printf("three\n");
}
return 0;
}
It prints
one two three
Sometimes falling through is what you want
Stacking cases with nothing between them is the neat way to say "any of these". This is the one time you *mean* to fall through.
C
#include <stdio.h>
int main() {
int day = 7;
switch (day) {
case 6:
case 7:
printf("Weekend!\n");
break;
default:
printf("School day\n");
}
return 0;
}
It prints
Weekend!
Try it yourself
What can a switch NOT check?
- An int
- A char
- A range like "between 1 and 10"
- A value from a variable
Answer it in the app
🔁 Do It At Least Once
The loop that always runs once
A while checks before it runs, so it might not run at all. A do/while checks afterwards — so the body always happens at least once. Note the semicolon at the end.
C
#include <stdio.h>
int main() {
int n = 10;
while (n < 3) {
printf("while ran\n");
n++;
}
do {
printf("do ran\n");
n++;
} while (n < 3);
return 0;
}
It prints
do ran
Counting with do/while
Otherwise it behaves like any other loop.
C
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 3);
printf("\n");
return 0;
}
It prints
0 1 2
The short-hand if
C has a one-line if for choosing between two *values*: condition, then ?, then what to use when true, then :, then when false.
C
#include <stdio.h>
int main() {
int age = 15;
printf("%s\n", age >= 12 ? "big" : "small");
int a = 4, b = 9;
int biggest = a > b ? a : b;
printf("%d\n", biggest);
return 0;
}
It prints
big 9
Try it yourself
When is do/while the right choice?
- Always
- When the body must happen at least once — like asking the player for input
- When counting down
- Never
What does this print?
C
#include <stdio.h>
int main() {
int n = 7;
printf("%s\n", n % 2 == 0 ? "even" : "odd");
return 0;
}
Answer them in the app
🔌 Working With Bits
Numbers are really rows of switches
Every number is stored as bits — 6 is 110, 3 is 011. C lets you work on those switches directly. & keeps a bit only when both have it, | when either does, ^ when exactly one does.
C
#include <stdio.h>
int main() {
int a = 6;
int b = 3;
printf("%d\n", a & b);
printf("%d\n", a | b);
printf("%d\n", a ^ b);
return 0;
}
It prints
2 7 5
Shifting doubles and halves
Moving every bit one place left doubles the number; one place right halves it, throwing away anything that falls off the end.
C
#include <stdio.h>
int main() {
printf("%d\n", 6 << 1);
printf("%d\n", 6 >> 1);
printf("%d\n", 1 << 10);
return 0;
}
It prints
12 3 1024
Keeping several yes-or-no answers in one number
This is what bits are really for. Give each switch a value that is a power of two, turn them on with |, and test them with &.
C
#include <stdio.h>
int main() {
int CAN_SWIM = 1;
int CAN_FLY = 2;
int CAN_RUN = 4;
int duck = CAN_SWIM | CAN_FLY;
printf("%d\n", duck);
printf("%d\n", (duck & CAN_FLY) != 0);
printf("%d\n", (duck & CAN_RUN) != 0);
return 0;
}
It prints
3 1 0
Try it yourself
Why is n << 3 the same as multiplying by 8?
- It is not
- Each shift left doubles, so three shifts double three times: 2 × 2 × 2
- Because 3 × 8 = 24
- Only for even numbers
What does this print?
C
#include <stdio.h>
int main() {
printf("%d\n", 12 & 10);
printf("%d\n", 12 | 10);
return 0;
}
Answer them in the app
🏆 Choice Master
Try it yourself
What does this print?
C
#include <stdio.h>
int main() {
int n = 2;
switch (n) {
case 1:
printf("a\n");
case 2:
printf("b\n");
case 3:
printf("c\n");
break;
default:
printf("d\n");
}
return 0;
}
Answer it in the app