Chapter 4. Basic Declarations and Expressions
A journey of a thousand miles must begin with a single step. —Lao-zi
If carpenters made buildings the way programmers make programs, the
first woodpecker to come along would destroy all of civilization.
—Anonymous
If you are going to construct a building, you need two things: the
bricks and a blueprint that tells you how to put them together. In
computer programming you also need two things: data (variables) and
instructions (code). Variables are the basic building blocks of a
program. Instructions tell the computer what to do with the
variables.
Comments are used to describe the variables and instructions. They
are notes by the author documenting the program so it is clear and
easy to read. Comments are ignored by the computer.
In construction, before we can start we must order our materials:
"We need 500 large bricks, 80 half-size bricks, and
4 flagstones." Similarly, in C++ you must declare
all variables before you can use them. You must name each one of your
"bricks" and tell C++ what type of
"brick" to use.
After the variables are defined, you can begin to use them. In
construction the basic structure is a room. By combining many rooms
we form a building. In C++ the basic structure is a function, and
functions can be combined to form a program.
An apprentice builder does not start out building the Empire State
Building. He starts on a one-room house. In this chapter you will
concentrate on constructing simple, one-function programs.
|