Beginner”s Guide to
Programming: Lesson 3
by Michael Simpson
This is the final lesson in the
Beginners Guide to Programming.
It is important that you read
and understand last month’s Lesson 2
before you proceed with this one. We
are going to cover the Branch, LookUp,
LookDown, and LCD commands.
Once you complete this lesson,
you should have enough of an understanding of the Basic programming
language to venture forth and explore
the world of microcontrollers.
Branch Command
Let’s say that you want to perform
a set of actions based on a number
between 1 and 5. One way to do this is
with an If statement as shown in Figure
1. If you single step through the code,
you will notice that each condition is
tested until a match is made. Even
after a match is made, we continue to
test until the end of the program is
reached. This is not a very efficient way
FIGURE 1
46 SERVO 09.2007
to perform this kind of action.
The Branch command will allow you
to branch to a particular location in the
code based on an index value. Take a
look at the code in Figure 2. The Branch
command uses the first argument as an
index to point to one of the labels that
you have provided. You supply a list of
labels that align with the index. In this
case, we used the labels do0, do1, do2,
do3, do4, and do5. You might ask yourself why we had to use the do0 label.
The Branch command is zero-based and
the first label that you provide is the
label that is jumped to if the index is 0.
The next is 1, and then 2, and so on.
Now, you might wonder what
happens if the index falls outside the
range of the provided jump labels. For
instance, what would happen if the
index value was 10? The Branch
command is designed to fall through to
the very next command if no label has
been provided for the given index. The
code in Figure 3 has been modified to
FIGURE 2
handle numbers outside the range of
the provided jump labels.
The Branch command is quite valuable, and when programming microcontrollers you will find it very efficient. Only
a single condition test is made, no matter how many jump labels are provided.
Using a little math, you can manipulate
the index so that larger values may be
used with only a few jump locations.
Later, I will show you how to do some
fancy jumping using the LookDown and
Branch commands together.
In older versions of the Basic
language, the Branch command was
called OnGoto.
Setting Simulator
to Intermediate
From this point on, you need to
have the Athena Simulator Mode set to
Intermediate. You do this by selecting
Intermediate from the Mode menu as
shown in Figure 4.
FIGURE 3