What if you have more than two possibilities? You can test for them one-by-one using else if. For example, imagine that a truck belongs to one of three weight categories: floating, light, and heavy.
if (truckWeight <= 0) { printf("A floating truckn"); } else if (truckWeight < 40000.0) { printf("A light truckn"); } else { printf("A heavy truckn"); }
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 don’t get a false positive. For instance, if you swapped the first two tests in the above example, you would never find a floating truck because floating trucks are also light trucks. The final else clause is optional, but it’s useful when you want to catch everything that did not meet the earlier conditions.