Begining Programming - Part II

Section Three, Language Elements

Review From Lesson I

NewsLetter TOC

Newsletter Home

Blast

MDI

OS Editor

LBW Review

Fast Data

SQLite

Tsunami

Begin Prog II

Locate/LBCard

Next we will be translating our flow chart above into actual program code. We will be building on the commands we learned in part one of this series. Let's review a bit of what we covered last time. Before we begin creating the program.

The PRINT command is used to get information out to the screen to communicate with the user.

The INPUT command is used to fetch data from the user. This collects the characters the user keys into the computer from the keyboard until the user presses the ENTER key.

CLS is a command that is user to clear all the contents of the mainwindow.

The GOTO statement allows the user to redirect the program execution from its current position to another area in the code identified by the label that the GOTO instruction specified. GOTOs allow the program code to loop - which the action of executing several statements then jumping back up to the first of those statements and executing them again.

The IF - THEN - END IF statements allow the program to examine the contents of a variable and conditionally execute certain lines of code. The examination is always in the form of an expression, which can be said as valueX is greater than valueY. The operator "greater than" is just one of several that can be used in an expression. The most common is the equals operator. The format of the IF - THEN - END IF statement is what we call a block of code. The IF and END IF commands form a sort of "book end" around the code that is to be executed only is the condition of the expression is satisfied. The format is as such:

If expression Then

{Basic statements}

End If

Variables - The Building Blocks of Programs

In addition to the commands above we also discovered Variables and Labels. Of all the concepts so far the variable is one of the most critical to understand. Much of programming is manipulating values stored in variables and comparing one variable to another.

A variable is like a box. It holds a specific piece of data. Each box has a unique name used to identify it by. The name can generally be almost anything. In Liberty Basic we suggest not using reserved words as variables. We will discuss these later. Some examples of variable names we will encounter in this article are:

Count

Card1

Selected

Answer$

Name$

I said variables are "like" boxes, but they are not really boxes. Rather, variables are specific named areas in memory that are set aside to hold data for us. As you may recall, in Liberty Basic there are two types of variables - Numeric and String.

Numeric variables hold values (numbers) that we can do math with. We could say that A=5 and B=2 (A and B are both numeric variables). If we assigned the value of A+B to the variable C (it looks like this: C=A+B), then C will be holding the number 7.

String variables can hold both numbers and letters (and a lot of other symbols too). The primary difference between a string variable is that when it hold a number you can not use it for arithmetic. That is because it really is not the number, but the character that represents the number. I know that is confusing, but take my word for it. Strings are expressed differently from numeric variables. The variable name always ends in a dollar sign ($). In the above example of variables the variables Answer$ and Name$ are strings variables.

Labels are used in your code to help organize it and to allow places for the code to branch or go to during program execution. A label is any word that is contained in brackets (i.e. "[" and "]"). Here are some examples of labels:

[loop]

[main]

[begin_here]

It is important to pay attention to the case used in typing your variable names and label names. In Liberty Basic the case counts for these elements. The variable Name is not the same variable at the variable name. Like wise, none of the following are the same: nAmE, NAME, namE. You get the point.

A New Command

In the code we are about to write we will be using a new command. It is actually a control structure. It is the FOR - NEXT loop. It is used to cycle through a block of code a specified number of times. The command syntax is as follows:

FOR variable = start TO end

{Basic statements}

NEXT variable

The FOR statement works like this: First time in it assigns the value of start to the variable named "variable" (you can call it what ever you want). Then it will cycle through the statements that are with in the code block (between the FOR statements and the NEXT statement), causing them to be executed each time through. It will increment the value of the variable "variable" each cycle. Once the value of variable is equal to the value of end the program will move on to the statement following the NEXT statement as the execution of the FOR loop will have finished.

Here is the example and description that is given in the Liberty Basic help file:

for var = 1 to 10

{BASIC code}

next var


In this case, the {BASIC code} is executed 10 times, with var being 1 the first time, 2 the second, and on through 10 the tenth time. Optionally (and usually) var is used in some calculation(s) in the {BASIC code}.

For example if the {BASIC code} is print var ^ 2, then a list of squares for var will be displayed upon execution.

Meet the Array

In our program we are also going to use a special form of variable called the variable array to simplify our code. Arrays can be a complex subject for newcomers to programming world. We will be brushing the surface just enough to use them in our program. An array allows you to have multiple instances of the same variable, but every instance of that variable is unique. What a concept! Let me explain it this way:

Imagine you are driving out on the edge of town and you come upon a mobile home park. As you glance at the driveway you notice a row of mailboxes There are 8 of them and the all look exactly alike. In fact they are exactly alike. They are all for 744 Pinewood Drive, they all are a half round tube design, they all are jet black and have little red flags. But one thing is different - each of them has a unique box number. They start at one and go up through 8.

You could say that that is an array of mail boxes. Every one of them is the same address. They each look the same, but they all have a unique element that identifies them as unique from the others around them. It is their box number. What is more, each one could have (and probably does have) unique contents. Just because they look the same and are all lined up together, it does not mean they have the things inside. This is also true of the variable array. Each array element can store its own unique value.

We will be using an array of a variable, not a mailbox. Yet both have very similar features in common. Each one has the same name. In the case of our program it is "cards" and each has its own number (which is shown as a sub-script). There are five of them, so these will be the variables:

cards(1), cards(2), cards(3), cards(4), cards(5)

We will explain arrays in much greater detail in our next installment, for now you have enough to wet your appetite and to allow us to create the program based on our flow chart.