Bunches o’ Numbers

The deepest, heaviest, most profound idea that you will learn from programming concerns the duality of thing and process. You can find an explanation of the concept on this page. Loopiness is a process-concept; now it’s time to look at the corresponding thing-concept: arrays of things represented by numbers. 

Every process works on some sort of thing. A shovel isn’t much good if you don’t have any dirt to dig. An oven is a tool that cooks, but you have to have something — food — to put into the oven. Loops work on arrays of numbers in the same fashion that shovels work on dirt and ovens work on food.

An array is just a list of numbers, like so:

1. 27.43
2. 89.17
3. 4788.2
4. 0.011
5. 1.01

Here’s a really simple example showing you how you set up an array and use it: [JavaScript: set up the array above, initialize its values, then print them out in a loop]

“Big, fat, hairy deal!” you say. “If I wanted to print out those five numbers, then I’d just print them out, like so:”

print(27.43);
print(89.17);
print(4788.2);
print(0.011);
print(1.01);

“No muss, no fuss, no complicated arrays or loops. Just the facts, ma’am. So why do I need arrays?” 

You’re right. You don’t need loops and arrays to print out five numbers. Neither do you need a bulldozer to dig a hole to plant your petunias. But don’t you want to do greater things than plant petunias? Computers are powerful because they can do lots of tiny steps over and over again. Not just hundreds of steps. Not just thousands. Not even millions. Computers can do BILLIONS of little tiny steps over and over again, really fast. For example, the display you are looking at this very moment utilizes that concept. The loop for the display is, schematically, something like this:

Start at the top left corner of the display
For all the horizontal lines in the display (maybe a thousand horizontal lines stacked one above the next) {
     For all the individual pixels on that line (maybe two thousand pixels from left to right) {
          Look up the array value of the itty-bitty pixel that appears there
          Draw that single pixel in the correct color
     }
}

That’s all there is to it. It’s ridiculously simple conceptually. But do the math: that inner loop executes two million times just to draw the display. And it does it maybe thirty times every second. That’s sixty million steps per second. 

That’s why loops and arrays are important. 

Arrays are must long lists of numbers. The list is ordered by what’s called an index. Here we run into one of those little details of programming that always confuses beginners:

People count 1, 2, 3; computers count 0, 1, 2

The computer way really is the right way, because 0 is just as much a number as 1, but most people have difficulty realizing this. Did you know that the numeral “0” wasn’t invented until about 1,500 years ago? Before that, people just didn’t count with zero. After all, why do we need a number for nothing? Still, it turns out that zero is reallly important for a lot of reasons. Otherwise, how would you know whether it was invented 15 years ago, 150 years ago, or 1500 years ago? 

There are deeper reasons for the importance of zero with computers, but I can’t explain them until after you’ve learned about transistors at the very end of this book. For now, you just have to suck it up and memorize the rule:

In computer programming, always count from 0, not from 1.

This means that the first member of an array has an index of 0, and the second index has an index of 1, and so forth. Yes, it’s a little screwy, but like I said, it makes sense once you understand what’s going on inside.