Advocacy

  Myths
  Press

Dojo (HowTo)

  General
  Hack
  Hardware
  Interface
  Software

Reference

  Standards
  People
  Forensics

Markets

  Web

Museum

  CodeNames
  Easter Eggs
  History
  Innovation
  Sightings

News

  Opinion

Other

  Martial Arts
  ITIL
  Thought


How Programming Works
Understanding the Basics

By:David K. Every
©Copyright 1999


 

Many people think programming is magic, or it is hard. It is neither. The basics can be described relatively quickly.

Programming is about creating fairly complex constructs, from very simple instructions. Then putting together many of those constructs together in ways to make more complex constructs. It is logical lego's. You keep building, using simple parts, until you have made something else out of those parts.

Don't be intimidated. If you could understand very basic algebra (x = 6 * 7), then you can get the basics of programming. There are three primary things to know (to get the very basics of programming):

If you can understand these three concepts, then you will get the very basics of most programming (and programming languages).

Variables

In the real world, we create words to communicate. What is a word? It can either be a written representation of something, or an audible sound that is also a representation of something. The sound or text for the word is not the actual object, it is just how we refer to that object. Welcome to the world of computer variables.

In programming I can create a variable to remember the state of something, or to represent something. I can do an assignment like:

x equals 100
 

NOTE: I am going to create a few examples using a mostly "English" syntax (or what is known as pseudo code). A computer Language's syntax may look a little different but the concepts are the same.

From that point on, x will represent the value of 100 (x becomes the word for 100). Sometimes you need to change what x represents, so later I can reassign the value with "x equals 200". The value changes, and x "varied" from its previous state (hence the name "variable").

C Syntax

Most Computer language are more symbolic (for space conservation). So we would use a symbol like '=' for equals, and other symbolic representations, and we usually end a line or statement with some special symbol (delimiter). But the concepts are the same.

In C the "x equals 100" statement would look like:

x = 100;

'X' is not a very descriptive variable (what is X?). So instead of x, lets use the variable "balance" to represent the balance in my checkbook. Then I can assign an initial value to balance (to mimic my real world balance), and over time I can add to balance, or subtract from balance, to match the balance that I have in the real world. So in a program, I use "balance" (the word) to represent the value for my balance. Simple association.

There are specialty variables that can't change (varried). For example the speed of light or the value of pi are constant -- so there is a specialty type of variable that can not change, that is appropriately called, a "constant". However, constants behave the same way as variables in every other way (you just can't change them).

Variable are the basis for all programming, and there are lots of ways to use them. Basically all a variable is, is just a reference to some location in the computers memory. The computer either gets the value (of those locations) or places into them, depending on what you are doing. The naming is just to help you out, rather that having to refer to everything by memory location. Pretty basic huh? It is.

There are a lot of other types of variables, and tricks to how they will be used. So there is a lot more than can be learning on variables, but it is not hard, and they all start with this premise.

Conditionals

In the real world, we do some things conditionally. We don't hit people UNLESS they are hitting us first. We stop our cars IF the light is RED (or else we go if it is green). While we have extra money in our accounts (or our wallets) we tend to buy things. Those are all conditionals in the real world, and that is basically the same concept in programming -- doing some things conditionally. The syntax isn't quite as "English" like, but the meanings are similar.

if the signal is green then go
or else if the signal is red then stop
or else if the signal is yellow then go really fast.

That is almost the exact same syntax as programming (and the exact same concept).

Programming Languages

The same example in C might look as follows:

if (signal == green) drivingCommand = go;
else if (signal == red) drivingCommand = stop;
else drivingCommand = goFast;

Pascal might look as follows:

if signal = green then drivingCommand := go;
else
   if signal = red then drivingCommand := stop;
   else drivingCommand = goFast;

First we check the conditional, then we assign the correct value to the "driving command".

There are some subtle differences (in Syntax) with symbols; differences between doing an assignment (putting something into a variable) and doing a test (getting something out of a variable), but you get the idea.

If you can read these sample snippets then you are already getting the basics of reading code (Computer Language).

Now some commands like "Stop, Go, GoFast" have to be defined elsewhere (as to what those will do, or what they mean). Sometimes those are just assignments (setting a value that will be checked later), and sometimes those will be sub-programms that will be their own set of actions. But you get the basic idea. (See Flow Control for more on sub-programs). There is nothing that complex about conditionals.

There are many different types of conditionals, each having its own purpose (specialty). Different languages alter the Syntax of conditionals -- but they are more similar than different (and there is a lot of commonality in even the Syntax). Conditionals include commands like, if-then-else, while-do, do-while, repeat-until, or case statements (in case of this.. or that...) and so on. But they are all still just different types of conditionals, and each one is pretty easy to learn.

Conditionals work based on a "conditional" of their own. They do a test (inside of conditionals) to see if the first statement is true. Conditionals work on something called Boolean logic (a statement is either true or false). "Is the signal equal to green?" is a Boolean test -- the first part of the statement will either be true or false, and the second part of the statement (the action or assignment) is ONLY done if the first part of a conditional is true. "Is 100 less that 50?", that statement would be false (100 is greater than 50), so the statement would stop (not do the remaining part). So this Boolean logic is actually how the statement itself works, and it is interesting that most people understand this concept already -- they know not to listen (or do) what you tell them, if the first part of a conditional is not met, and computers understand this too.

Hopefully you get the idea about conditionals, and what they are (and even how they work). They just test, and act conditionally (based on the results of the test). That's it.

Flow Control

Programs normally just flow down the page (top to botton, just like we read -- one line to the next). However, sometimes you want to be able to change the "flow" of the program -- have it go somewhere else. Like reading a book, and the beginning it telling you stuff you alread know, so you "jump" (go to) the next chapter. Or sometimes you don't know what something means, so you temporarily look it up in the glossary (and return when done). Both of those are "flow-control" for reading.

Since programs have a lot to do, there can often be some commonality (the same code in more than one place). So flow control is also used to "collect" or group commonality (commonly done actions) into little sub-programs. Flow control is also used with conditionals -- where you want to skip over some instructions (if a test is false) -- so you have to interrupt the programs "flow" (not execute the next instruction), and instead go somewhere else. So there are many uses for this flow control -- but lets break them down to their most common uses.

Subroutines: Lets say you wanted to make a small sub-program that controlled "stopping" a car. This "sub-program" is often called a subroutine (or a Function, Procedure, Method, and so on -- all are names for the same thing). Subroutines are ways that you group functionality into a small program, and give that sub-program a name / label. Then, whenever you want to execute that sub-program, you just put that routine name in the code (sometimes prefixed with a command like "call", "gosub", etc.), and the computer knows to jump to that routine (interrupt its normal flow and goes somewhere else), and the routine will tell it to "return" when it is done (and return to where it was called from when called). By doing this, you can write a routine that does a common action once, and then do it from elsewhere (without having to put the same statement in a thousand different places in the code.

So lets create a little routine. We should probably name (label) the routine (so we know what to call it), then define what it will do. (This is in no real syntax, but should give you an idea).

Stop Routine:
gasPedal = off;
clutchPedal = on;
brakePedal = gentlyDown;
 
if (notGoingToMakeStop = true) then
    brakePedal = firmlyDown;
 
if (stillNotGoingToMakeStop = true) then
    armsOverFace = true;
    wetSelf = true;
    call curse routine;
    call flashLifeBeforeEyes routine;
 
return;

So, this subroutine (called Stop) could be called from anywhere else in the program. Notice that this routine may also call other routines (like curse, or flashLifeBeforeEyes). Because of subroutines programmers can create more and more complex programs, out of many simpler parts. This is one of the most common forms of flow control.

Loops: Another very common form of flow control is the loop. Many times a programmer wants to do something, many times over. Lets say in the previous example the "curse" routine was supposed to say, "damn it!" one hundred times (after all, you are about to wreck your car). It could be done without a loop in the following way --

Curse Routine:
say "Damn it!";
say "Damn it!";
say "Damn it!";
.
. (96 more of them)
.
say "Damn it!";
 
return;

Not a very exciting routine -- and rather large to do something pretty basic. But by using a loop (flow control to make the program automatically executes the same set of instructions, many times over), we ca simplify the routine. We can create our own loops (with conditionals and subroutines) -- but that can get a little complex (and time consuming). Since looping is so common in programming, computer languages have many different build in loop commands. A common type of loop is the "For-Next" loop -- which means do something for a number of times.

Curse Routine:
for count = 1 to 100;
    say "Damn it!";
next;
 
return;

Count starts at 1. When the computer gets to the "next" statement, it goes back to the "for" statement, increments count by one, and goes on. This repeats until the count equals 100 (the "to" condition is met) -- then the next statement does nothing, and the program continues on its merry way. (Notice how much easier, and smaller, this is compared to doing a 100 "say" statements).

Another way of approaching the problem would be with a more generalized loop statement, like the repeat-until loop:

Curse Routine:
count = 0;
repeat;
    increment count;
    say "Damn it!";
until count=100;
 
return;

Every time the computer gets to the until condition, it just checks the condition is met, if it is not met, then it jumps back to the repeat . Basically it does just what it sounds like -- it repeats until the condition is met. This is more generic form of loop, since the conidition can be just about anything, and how you set count can be done in many different ways (not just a simple increment).

Of course there are many types of loops (for-next, repeat-until, do-while, while-do, and so on) -- most of them are just better suited to doing certain things. But the point of all loops, is to loop the flow control while something is true, or until some condition is met.

Goto: There is simpler form of flow control called the Goto (Go To). This is just like calling a subroutine, without there being a return. The flow control jumps somewhere else in the code (which is usually identified by a name/label or line number). It is also sometimes referred to as a jump -- because you are doing just that, jumping ahead in the code, or jumping back.

The Goto has gotten a bad rap in programming, basically because of really poor usage. In in most "higher level" (easier to use) languages, the Goto has been "hidden" (or is frowned on). In lower level languages (closer to what the machine understands) most instructions are still use "goto's" (or jumps) to achieve things. Think of the "until" statement in the above example, that statement is literally the same as the following:

if (count < 100) then Goto Repeat;

As long as the count is not 100, then we are going to keep "going to" the place in the code where the repeat loop started. So the Goto is still part of the language, it is just hidden.

Subroutines are also made of Goto's. The program "goes to" another place, but passes a parameter as to where it came from. Then the return just "goes back". So jumps or goto's are used everywhere, they are just being hidden in most modern (high level) syntaxes.

So those are the basic types of flow control -- Jumps (Goto's), Loops, and Subroutines.

Conclusion

Did that seem easy?

If you understand the concepts of variable, conditionals and flow control, then you understand the (very) basics of programming in ANY computer language.

There are more commands -- but they are variations in these categories.

Languages (and Operating Systems) add many pre-written routines (often many thousand) -- all to make programming easier. This is basically a way to add in your own commands as you go (to help solve your particular problems). But the concepts of programming are pretty basic (1).

(1) If you understand the basics of plumbing, wiring, and mechanics (a little welding and mechanisms), then you get all the basics for cars too -- but that doesn't mean you are completely prepared to be an auto designer or mechanic, but the systems themselves are not hard (in concept).

It takes a while to get all the possible commands (for a Syntax or OS) down, and it takes experience to learn what mistakes will be made and how to find/fix them -- but programs are still just lots of simple statements (like the ones above). The complexity is that programs can be MILLIONS of lines of code (simple statements). Furthermore those programs are often working with, and calling subroutines, that are part of an Operaticng System -- which are MILLION of more lines of those simple statements as well. To make things tricky, one part of the program that messes up a variable, can only cause an error thousands or millions of instructions away -- making it very hard to find there errors (bugs). So the complexity is not in the basics, which are easy, but in all the details.

At least now you can appreciate what is going on.

If you have an interest in doing some programming, and don't know where to start, then read "Programming: Where to Start".


Created: 01/19/97
Updated: 11/09/02


Top of page

Top of Section

Home