A Beginner's Guide to D/Short Introduction to Programming

From Wikibooks, open books for an open world
Jump to: navigation, search

What Is Programming[edit]

Computers are machines that simply execute the instructions given to it by a program, and programming is the process that creates the set of instructions that make up a program (or part of one). Computers do not think in the same sense that we do, they do not ponder the instructions given to them, nor do they decide to disregard them or alter them. Computers blindly follow the set of instructions, that you as a programmer give it.

And this is what makes programming a challenge. It is not so easy to create the exact set of instructions to get a specific task done.

How to Cook a Pancake[edit]

Let's tell a really, really stupid, but capable, person how to cook pancakes.

  1. In a bowl, put in a cup of flour, one egg, and enough milk to make a smooth mixture.
  2. Mix it with a wooden spoon until it is smooth.
  3. Heat a frying pan with a small knob of butter until melted.
  4. Place a cup of the mixture into the frying pan and cook one side until bubbles form.
  5. Carefully turn the half-cooked pancake over and continue cooking the other side until it is firm.
  6. Place cooked pancake on a plate and repeat these steps until all the mixture is used.

Simple and obvious, no?

No, not really. For example, I didn't say to tip the flour out of the cup into the bowl, so a computer would have done literally what I said to do - "put a cup of flour" into the bowl. Then there is the egg; I didn't say to crack the egg and only put its contents (yolk and white) into the bowl, so the shell would have also gone in. And what exactly is a "smooth" mixture?

Further literal execution of these instructions would have the computer trying to mix a bowl rather than the contents of the bowl. And this assumes that it knows what "mixing" is! And have you ever tried to use a knob of butter to heat a frypan? And what is a "knob" of butter? Will the computer wait until the butter is melted or the frypan is melted? Again with the cup; are we cooking it or its contents? How many bubbles do we wait for - two, three, ... ? What if no bubbles show up - do we keep cooking it while it rapidly burns to a cinder? The last instruction tells us to repeat but literally that includes all the steps, in a never ending frenzy of pancake making.

I don't mean to scare you but it is important to realize that computers take your programs literally, and thus you have a responsibility to be accurate and precise. That is the challenge of programming.

What is Programming (revisited)[edit]

It is the process of writing algorithms and describing data so that a computer can correctly perform the task required of it.