Think about the electricity bill that reaches your home each month. The meter gives the company one number, the units you used, and a program must turn it into your bill. That takes two kinds of work. It must calculate: multiply units by a rate, add slabs together. And it must choose: the first hundred units cost one rate, the next hundred a higher one, everything above that higher still, so for every unit the program must decide which rate applies. (The rates in this chapter are made up, but the shape is exactly what a real billing program does.) Calculating and choosing: that is the whole of this chapter.
Last chapter ended with a program that read a radius, worked out an area and printed it. Every line ran once, in order, whatever the user typed. Now the program gets two new powers: the operators, which calculate and compare, and the decision statements if, else if and switch, which let it take one road or another.
By the end you'll be able to work out 17 / 5 + 17 % 5 without running it, and say why 7 / 2 is 3 even when stored in a double. You'll know when C converts a value by itself and when you must ask it with a cast. You'll read a condition and know whether it gives 1 or 0, and why a && b sometimes never looks at b. You'll use ++, += and the bit operators without mixing up = with == or & with &&, and you'll know which operator goes first in any expression. You'll write decisions with if and switch, including the traps that make exam outputs surprising: an else that joins the wrong if, a case with no break. And you'll put decisions to work in two programs written papers love: a slab-wise bill and the leap year.