Home -> Programming -> Planning -> Program Flow

Program Flow

When we write programs we are trying to control the machine to make it do what we want it to do. To achieve this end several ideas and tools have been developed. First, in importance but not historically, we learned to break up our code into smaller chunks much like a story is broken up into paragraphs. Doing this not only improves the readability of the code as paragraphs do for a story, but also makes it easier to find errors in our code, and fix them. Thus these chunks make it easier to maintain our code. Second, we learned that the order in which we arrange our programming commands determines the order in which they are executed. This is called Sequence. Third, we need to be able to make decisions to cause our code to branch based on information we have in our program or receive as input from the user. Fourth, we often find ourselves repeating the same instructions over and over. Since manually writing out code that repeats is repetitious and wasteful, we write our code to have the computer handle the repetition thereby saving space, time and coding. These code structures are collectively called loops.


A statement is a directive to the program to perform one action. In the plural we refer to several actions which accomplish a set of actions. Sets of actions can be created either informally or formally. Informal divisions of code are created by leaving space between lines of code. We often call these code segments because they perform an identifiable task. One caution, what one programmer thinks of as an identifiable task is not necessarily the same as that of another programmer and this is not really a problem. Code segments are analogous to paragraphs in writing. The formal separations of code are analogous to sections in writing. They are often composed of several segments which are combined to perform some larger action. In Visual BASIC the formal chunks are named as follows:


The central idea here is that some operations must follow in a particular sequence in order for them to work properly. When going home to a locked house, we must unlock the door before we can open it, and open the door before we enter. The same sequential dependencies exist in programming and must be learned and followed in order to get the desired results. In principle this is a simple concept, Refer to the Algorithms page for a more complete discussion of sequence. The concept is fundamental to all programming.


This is the programming term for decision which must be made as we process input into output. We use four main decision structures to make decisions. They are:


When we have to do the same thing repeatedly or, as is more usual, a similar thing over and over again, we use iteration or looping. For example if we write a program to count to 10 in original BASIC, it looked like this:

Print 1
Print 2
Print 3
Print 4
Print 5
Print 6
Print 7
Print 8
Print 9
Print 10

Now this is a very functional program - it works - and it produces the desired output. However, if we wish to extend our program, say make it count to 1,000,000 you should recognize that you do not want to program this new project in the same way that we did the one which only counts to 10. It would be a mammoth undertaking and would have little practical application for all the time spent creating it!

If you look closely at the previous program you should also be able to see that we are essentially repeating the same step 10 times - printing a number to the screen. The problem is that the repetition is not exact since we want to print a different number each time. At this point, you hopefully remember the lesson on using variables. They exist for exactly this type of situation. Instead of using literals (the numbers themselves) we can use a variable (a container for the number) and now each step is exactly the same.

Print number

Now we can rewrite our program to use this new method. We would also use conditional decision making from the above section to recognize when we reach our last number. It would now look something like this:

    Print number
    number = number + 1
until number > 10

Not only is this program just as effective as the one above, but it is also more efficient! We can count to 10 with only 4 lines of code instead of 10 lines of code. This program is also easier to extend, we have only to change the literal 10 to a different value and our program can count as high as we like. now a project to count to 1 million is child's play.

There are two types of loops: pre-test and post-test. To decide which one to use we have to know what the minimum number of possible iterations should be:


When we have several statements which are used together we can place them inside a code module called a subroutine. The advantage of subroutines is that they can be repeatedly called and used but they are only typed into the program once. This reduces the number of lines of code keeping our program smaller. By having the code only appear in one place rather than many places it is easier to maintain. We only have to change it once as opposed to changing it in many places.