November 2013
Beginner
325 pages
9h 47m
English
What if you have more than two possibilities? You can test for them one by one using else if. For example, suppose a truck belongs to one of three weight categories: floating, light, and heavy.
if (truckWeight <= 0) {
printf("A floating truck\n");
} else if (truckWeight < 40000.0) {
printf("A light truck\n");
} else {
printf("A heavy truck\n");
}
You can have as many else if clauses as you wish. They will each be tested in the order in which they appear until one evaluates as true. The “in the order in which they appear” part is important. Be sure to order your conditions so that you do not get a false positive. For instance, if you swapped the first two tests in the above example, you would never find a floating truck ...
Read now
Unlock full access