You only need to learn five things to write computer programs:

  1. Variables, which are named boxes to put information in
  2. Operators, which do things to pieces of information, like add them together, subtract them, multiply them and so-on
  3. Conditionals, which are rules for making decisions, expressed in “if… then… else…” form
  4. Loops, which say what must happen every time an imaginary wheel turns
  5. Function calls, which put a name to clusters of the above so you can tidy up your work

Everything else in computer programming is built out of these things and understandable in terms of these things. When a programming language gives you the above five things, we give it a name in honor of the man who figured out these basics: we say it’s Turing equivalent. All of the different programming ideas and models, like “Object Oriented Programming”, and “Functional Programming” and “Aspect Oriented Programming” are all Turing equivalent inventions that are meant to make it easier to write programs and make use of them.

This also applies to all of the books and blog postings about computer programming, because they are either teaching how to use a set of function calls assembled together into a library, or the most advisable strategy for making your own library of function calls, or how to organize a team of programmers who are making programs and libraries of function calls.

Information and variables

Information is always numbers to a computer, but often words to us. The metaphor that programmers are taught is of a cardboard box with a label on it, and the label is the variable. The cardboard box is misleading, because in the computer’s memory it’s like a single massive cardboard box, and on the edges someone has used a marker to draw dividing lines. A variable simply applies a temporary label to one of those marked divisions, along with a rule for figuring out how many divisions that name covers.

Strings

Words, sentences, paragraphs and books are numbers linked together like sausage links into strings. To a computer they’re opaque in meaning and processed like a sausage machine chopping them into segments according to one rule or another. I could say, “break up this string by its spaces” and get a list of all the words in a sentence, or I could say “break it up by hyphens” and get the three numbers in a Social Security number. For example, if the input to such a program was “066-29-5815” then it would produce three discrete values like this:

  1. 066
  2. 29
  3. 5815

The computer doesn’t care what the meaning of either the input or the output is, so if I give it the first sentence of A Tale of Two Cities and ask it to break it up by hyphens like a social security number, the computer will shrug and return an array with one element containing:

  1. “It was the best of times, it was the worst of times.”

Not like I didn’t know what I was doing, eh? This is the first example of, and the essence of a bug. A bug is the result of not preparing the computer for what it would be told. Just like telling your pregnant wife that you’re sterile, you may have a result that’s catastrophic for both of you. Bugs are dealt with by regulating the input and broadening the scope of what the program can cope with.

variable is called such because it’s a name that belongs to something that can change, or vary. Someone called Trevor Lewis may be a schoolboy in 1989 and a musician in 2009 because they change. I can put the first letter of A Tale of Two Cities into a variable, or the entire book, because a variable is nothing more than a name for the location of the very first letter in the computer’s memory. Implied are all the rules for figuring out where the book ends and the other things stored in memory begin.

Operators

Addition, subtraction, multiplication and division are the basic operators as we know them. There’s also a special kind of operator called assignment which puts the result of an operation into a variable. When you sit down to program a computer, you use a language that classifies variables by type, which helps it figure out how to apply those operators. If you add two numbers it uses arithmetic, but if you add two strings then it uses concatenation. In boldcase are operators:

  • + 2
  • “Trevor” + ” ” + “Lewis”
  • WeeklySalary = AnnualSalary / 52

For the third example hark back to the first concept: variables. If I put words inbetween quotes, I’m giving you a string. But if I use a name without quotes, I’m talking about the name I gave to a variable. Lets pretend I made a variable named AnnualSalary and assigned a value of 52,000 to it. In the third example, the information I stuck to that that label was divided by 52 and stashed into a second variable that I named WeeklySalary. The equals symbol ( = ) was used as an assignment operator. The variable WeeklySalary now contains the number 1,000.

Conditionals

A conditional is a rule for making a decision, and it uses operators. You’re walking down a road, and you meet me just as you come to a fork in it, and I say:

  • 4 == 2 + 2

(I’m going to use two equals signs to distinguish between the act of stuffing information into a variable (one equals sign) and comparing two expressions to see if they agree (two equals signs))

After being given that statement, you should agree with me: “that’s true”, you might say. And since you’re playing the role of the computer in this play, I’ll respond with:

“Okay, since you agree, then take the right fork in this road you’re walking down.”

And if that was all there was, then our program would forever take the right-hand fork, as if there was no other possible choice; the computer is the ultimate straight-man, forever providing the setup for a punchline, But before conditionals we had introduced variables, and variables combined with conditionals give you simple but operational programs. Because if the above seemed to be too stupid to be real, then try this:

  • NowInYears – YourBirthdayInYears >= 21

This is the computer program running the robot bouncer guarding the entrance to a bar. If NowInYears is storing 2009 and YourBirthdayInYears is storing 1989 then you’re not taking the right-hand path into the bar, because 2009 – 1989 = 20, which is less than 21. The test that the conditional is built on fails.

Loops

Computers were invented to perform repetitive tasks accurately, and computer programs spend most of their runtime in a loop with some kind of rule to tell it when to quit after all the work is done. What happens in the loop can be any of the five things covered here, including having more loops nested within larger loops. 

Imagine a man calls his secretary and says “Gracie, give me the file of every customer in Manhattan who spends more than $5000 a month with us.” Poor Gracie has to go through every file in the cabinet and do the same thing, which is to check if their address is in Manhattan (a conditional) and then go through all of their orders to calculate their average monthly purchase. A loop within a loop.

There are two famous ways to write a loop: increment a counter until it counts up to a limit, and perform an operation for each value in a list. The first kind are useful if you need to know what position in the loop you’re at. For example:

  • for (i = 1; i <= 100; i++)

It’s setting a variable–used as the counter–to begin at 1 in the first clause. In the second clause it’s saying that the loop should keep going as long as the counter is less than, or equal to 100. The third clause tells it how you like the counter to be incremented. If I want to jump two steps at a time, I could change the third clause like this:

  • for (i = 1; i <= 100; i = i + 2)

Now it’ll loop 50 times. This is useful when I want to use the value of the counter for something, perhaps as an index position to look something up. When the above line is run, the variable called i will jump: 1, 3, 5, 7, 9, 11, and so-on.

Superior to the above–but only when one doesn’t need to know one’s position in line–is foreach. This method is available in modern languages because it’s ridiculously convenient and avoids a kind of bug called the “off by one” error. It revolves around anything that looks like an array or collection of variables.

Collections

The primitive collection is an array, which is the cardboard box with a block of divisions marked off. Each division contains a unique value that is respected in itself. Grandma’s pill box, with one compartment sectioned off for each day off the week, is an array.

More kinds have been invented, but their peculiarities are not just beyond the scope of this tutorial, but nowadays beyond the attention of most programmers. You are lucky to be in command of machines with gob-smacking amounts of memory, and the menagerie of collection types (with silly names like “arrays” and “linked lists”) have been tamed by the languages you’re likely to meet. If their genus should ever become a matter of concern to you, it’ll be because you’re operating on a speciality platform–like the embedded operating systems of microwave ovens, wristwatches, and ballpoint pens.

Categories

Wait, does the nav block sit on the footer for this theme? That’s bold.

Software Engineering Tips

Yet another collection of rubbish mostly focused on C# and IT development

Explore the style variations available. Go to Styles > Browse styles.