November 2013
Beginner
325 pages
9h 47m
English
It is not uncommon to check a variable for a set of values. Using if-else statements, it would look like this:
int yeastType = ...;
if (yeastType == 1) {
makeBread();
} else if (yeastType == 2) {
makeBeer();
} else if (yeastType == 3) {
makeWine();
} else {
makeFuel();
}
To make this sort of thing easier, C has the switch statement. The code above could be changed to this:
int yeastType = ...;
switch (yeastType) {
case 1:
makeBread();
break;
case 2:
makeBeer();
break;
case 3:
makeWine();
break;
default:
makeFuel();
break;
}
Notice the break statements. Without the break, after executing the appropriate case clause the system would execute all the subsequent case clauses. For example, if you had this: ...
Read now
Unlock full access