Writing Computer Code
Book image
Explore Book Buy On Amazon

Computers don't do anything without someone telling them what to do, much like the average teenager. To make the computer do something useful, you must give it instructions in either of the following two ways:

  • Write a program that tells a computer what to do, step by step, much as you write out a recipe.
  • Buy a program that someone else has already written that tells the computer what to do.

Ultimately, to get a computer to do something useful, you (or somebody else) must write a program.

A program does nothing more than tell the computer how to accept some type of input, manipulate that input, and spit it back out again in some form that humans find useful. Table 1 lists some common types of programs, the types of input that they accept, and the output that they produce.

Table 1 Input and Output for Various Programs

Type of Program

Input

What the Program Does

Output

Word processor

Characters you type from the keyboard

Formats the text; corrects spelling

Displays and prints neatly organized text

Game

Keystrokes or joystick movements

Calculates how fast and far to move a cartoon figure on-screen

Moves a cartoon figure on-screen

Stock-market predictor

Current and past prices for stocks

Tries to recognize trends in a stock's price fluctuations

Predicts the future price of a stock

Missile guidance program

Current location of the missile and the target

Calculates how to make the missile's location and the target's location coincide

Corrects the trajectory so that it stays aimed at the target

Optical character recognition (OCR)

Text from a scanner

Recognizes shapes of characters

Converts scanned text into a text file that a word processor can edit

Web browser

HyperText Markup Language (HTML) codes on other computers

Converts the HTML codes into text and graphics

Displays Web pages on-screen

Programming is problem-solving

Essentially, a program tells the computer how to solve a specific problem. Because the world is full of problems, the number and variety of programs that people can write for computers is practically endless.

But to tell a computer how to solve one big problem, you usually must tell the computer how to solve a bunch of little problems that make up the bigger problem. If you want to make your own video game, for example, you need to solve some of the following problems:

  • Determine how far to move a cartoon figure (such as a car, a spaceship, or a man) on-screen as the user moves a joystick.
  • Detect whether the cartoon figure bumps into a wall, falls off a cliff, or runs into another cartoon figure on-screen.
  • Make sure that the cartoon figure doesn't make any illegal moves, such as walking through a wall.
  • Draw the terrain surrounding the cartoon figure and make sure that if the cartoon figure walks behind an object such as a tree, the tree realistically blocks the figure from sight.
  • Determine whether bullets that another cartoon figure fires are hitting the player's cartoon figure. If so, determine the amount of damage, how it affects the movement of the damaged cartoon figure, and how the damage appears on-screen.

The simpler the problem is that you need to solve, the more easily you can write a program that tells the computer how to work. A program that displays a simple Ping-Pong game with two stick paddles and a ball is much easier to write than a program that displays World War II fighter airplanes firing machine guns and dropping bombs on moving tanks while dodging anti-aircraft fire.

Programming isn't difficult; it's just time-consuming

Programming really isn't that difficult or mysterious. If you can write step-by-step instructions directing someone to your house, you can write a program.

The hardest part about programming is identifying all the little problems that make up the big problem that you're trying to solve. Because computers are completely stupid, you need to tell them how to do everything.

If you're giving a friend instructions to get to your house, for example, you may write down the following information:

1. Go south on Highway I-5.

2. Get off at the Sweetwater Road exit.

3. Turn right at the light.

4. Turn left into the second driveway.

Of course, if you try giving these instructions to a computer, the computer will get confused and wants to know the following additional information:

1. Where do I start and exactly how far south do I drive down Highway I-5?

2. How do I recognize the Sweetwater Road exit, and how do I get off at this exit?

3. After I turn right at the light, how far to the right do I turn, and do you mean the traffic light or the streetlight on the corner?

4. After I turn left into the second driveway, what do I do next? Park the car? Honk the horn? Gun the engine and accelerate through your garage door?

You need to tell computers how to do everything, which can make giving them instructions as aggravating and frustrating as telling children what to do. Unless you specify everything that you want the computer to do and exactly how to do it, the computer just plain won't do what you want it to do.

About This Article

This article can be found in the category: