| I l @ ve RuBoard |
1.3 How to Learn C++The only way to learn how to program is to write programs. You'll learn a lot more by writing and debugging programs than you ever will by reading this book. This book contains many programming exercises, and you should try to do as many of them as possible. When doing the exercises, keep good programming style in mind. Always comment your programs, even if you're doing the exercises only for yourself. Commenting helps you organize your thoughts, and commenting your own programs is good practice for when you go into the "real world."
Don't let yourself be seduced by the idea that, "I'm only writing these programs for
Finally, programs tend to be around far longer than expected. I once wrote a highly system-dependent program that was designed to work only on the computer at Caltech. As I was the only one who would ever use the program, it would print the following message if I got the command line wrong: ?LSTUIT User is a twit
A few
Imagine how horrified I was when I came into the Computer Science office and was accosted by the chief secretary. This lady had so much power she could make the dean cringe. She
Sprinkled throughout are not only examples of working programs (to show you how to do things), but also examples of broken programs where we ask you to go through the program and figure out what's wrong. Often the problem is very subtle, such as a misplaced semicolon or use of = instead of == . These programs let you learn how to spot mistakes in a small program. That way when you make similar mistakes in a big program ”and you will make mistakes ”you will be trained to spot them. |
| I l @ ve RuBoard |
| I l @ ve RuBoard |
Chapter 2. The Basics of Program Writing
Computers are very powerful tools that can store, organize, and process a tremendous amount of information. However, they can't do anything until someone gives them detailed instructions. Communicating with computers is not easy. They require instructions that are exact and detailed. Wouldn't life be easier if we could write programs in English? Then we could tell the computer, "Add up all my checks and deposits, and tell me the total," and the machine would balance our checkbooks.
But English is a lousy language when you must write exact instructions. The language is full of ambiguity and imprecision. Grace Hopper, the grand old lady of computing, once commented on the instructions she found on a
She tried to follow the directions, but she ran out of shampoo. (Wash-rinse-repeat. Wash-rinse-repeat. Wash-rinse-repeat. . . .) Of course, we can try to write in precise English. We'd have to be careful and make sure to spell everything out and include instructions for every contingency. If we worked really hard, we could write precise English instructions, right?
As it turns out, there is a
Still, even with all the extra verbiage the government puts in, problems can occur. A few
The law had two requirements: 1) that motorcycle riders have an approved crash helmet and 2) that it be firmly strapped on. The cop couldn't give the motorcyclist a ticket because the man did have a helmet firmly strapped on ”to his knee. So English, with all its problems, is out as a computer language. Now, how do we communicate with a computer?
The first computers cost millions of dollars, while at the same time a good programmer cost about $6,000 a year. Programmers were forced to program in a language where all the instructions were reduced to a series of
1010 1111 0011 0111 0111 0110 .. and so on for several hundred instructions
Whereas machines "think" in numbers, people don't. To program these ancient machines, software
A typical program might look like: Program Translation MOV A,47 1010 1111 ADD A,B 0011 0111 HALT 0111 0110 .. and so on for several hundred instructions
This process is
Figure 2-1. Assembling a program
Translation was a difficult,
He showed his new creation to his boss and was immediately chewed out: "How dare you even think of using such an expensive machine for a mere `clerical' task?" Given the cost of an hour of computer time versus the cost of an
Fortunately, as time passed the cost of programmers went up and the cost of computers went down. So it became more
Assembly language organized programs in a way that was easier for the programmers to understand. However, the program was more difficult for the machine to use. The program had to be translated before the machine could execute it. This was the start of a trend. Programming languages became more and more
Over the years a series of high-level languages has been devised. These languages are attempts to let programmers write in something that is easy for them to understand and also precise and simple enough for computers to understand.
Early high-level languages were designed to handle specific types of applications. FORTRAN was designed for number
Later on, Brian Kernighan and Dennis Ritchie developed C and Bjarne Stroustrup turned it into C++. |
| I l @ ve RuBoard |