Chapter 2: Statements |
The purpose of programming is to tell the computer what to do. Computers are better at doing some things than you are, like a chef is better at cooking than you are. It’s easier to tell a chef to cook you a meal than to cook it yourself. The more precise you are in your request, the more your meal will turn out how you like it. In most scenarios like this in real life, small amounts of ambiguity and misinterpretation are acceptable. Perhaps the chef will boil your potato before mashing it instead of baking it. With computers, however, ambiguity is rarely acceptable. Programs are so complex that if the computer just guessed at the meaning of ambiguous or vague requests, it might cause problems so subtle that you’d never find them. Programming languages, therefore, have been designed to accept only completely clear and unambiguous statements. The program might still have problems, but the blame is then squarely on you, not on the computer’s guess. Much of the difficulty in programming comes from having to be perfectly precise and unambiguous about everything.
Or, as my friend Dan Collens puts it, “Writing software is like writing instructions for a bratty 12-year-old who’s trying to take everything you say exactly literally just to annoy you.” Keep this in mind when you find yourself saying, “Yes, but what I meant was . . .”
Some languages require total and complete detail about everything. C and C++ are such languages, and are called low-level languages. Other languages will make all sorts of assumptions, and this lets the programmer specify less detail. Python and Basic are such languages, and are called high-level languages. In general, high-level languages are easier to program but give you less control. Control is sometimes important, for example if you want your program to go as quickly as possible. Most of the time total control and speed aren’t necessary, and as computers get faster, high-level languages become more popular. Here we will deal exclusively with high-level languages. Low-level languages are generally similar except there’s often more lines of code to write to accomplish the same thing. (Code is short for program code, a term that refers to the text you write to tell the computer what to do when the program is launched.)
Note that less detail does not mean less precise. All languages require precise writing. To go back to the cooking analogy, a high-level language is like asking, “I want two cups of mashed potatoes,” whereas a low-level language requires you to specify exactly how to make the mashed potatoes. The high-level language has a specification that spells out exactly how those mashed potatoes will be cooked, so there’s still no guesswork on the part of the computer.
Chapter 2: Statements |