BWD Chapter 04: Programming concepts
2004-10-28 17:15 - Beginners Web Development
Chapter 04
Programming concepts
We have already introduced two important concepts, the first being the statement. The print statement that we used in the hello world program consisted of one command and one argument. The command was print, which displays or prints text on the screen, and the second was "Hello world" which was the text that we wanted to display.
I also snuck in another important concept without even mentioning it, the constant. A constant is just what the word says, a value that is constant, unchanging. When we put our phrase inside the double quotes, it was a constant, nothing about running the program ever changed the value there. We also hinted at numbers, numbers do not need quotes to say where they start and stop. A number starts with the first character that is a digit, and ends with the next character that is not a digit (or decimal point). In fact, if you put a number inside double quotes, it becomes a string, and not a number. This is important becausw numbers and strings are different, and can be used in different ways.
One of the simplest and still most important things about numbers is that you can do math with numbers. To perform mathematical operations like addition and division, we use operators. There are many kinds of operators and each must me used in a specific way. Operators can be unary or binary. Unary operators perform an action with just one number. Putting a - sign in front of a number turns it into a negative instead of a positive value. Binary operators function properly only with two numbers. A + sign between two numbers means the addition of the two numbers.
print 3 + 8 * 2
There is also an order of operations, just like back in math class. each operator has a specific meaning and a particular order that it should be used in. Note above the asterisk symbol, this means multiplication. The statement above prints 19, 8*2=16, 16+3=19.
BASIC has the following operators:
+ addition
- subtraction and negation
* multiplication
/ division
^ exponentiation
But wait! These are the arithmetic operators, there is another kind as well. The are called comparison operators which are clearly used to compare values rather than to modify them. They are the following:
= equality and assignment
< less than
<= less than or equal to
> greater than
>= greater than or equal to
<> not equal to
Note that comparison operators always have the lowest order of operation. Any other operators in the statement are evaluated first. But wait, I snuck the biggest secret yet in above, did you notice? It is that pesky equals sign. Not only does it test for equality, it also creates it through assignment. It's time to meet the constant's big brother, the variable.
As we said, a constant is a non-changing value. It is something we type directly into the program when we are writing it, and it does not change when the program runs. A variable is also a value, but it varies or changes. Though we can assign a particular constant value into a variable, when we run the program we can change the variable. Or to be more accurate, we can change what value the variable holds. A variable is a name for a value, and we use the name because the value can change, but the name stays the same.
Let's re write our hello world program to use variables and operators. The new program will do the same thing, but in a different way. We have just learned about variables and operators, we will use an operator to assign a value to a variable, then display the contents of that variable on the screen. So how do we assign a value to a variable?
You probably noticed above that I called = an assignment operator, and figured we would use it. You're right! But we need a variable to assign it to. In some languages, you must define all variables to be used before you use them. This is often very good for planning and organization, because you are forced to. Breathe easy though, BASIC does not do this. What you do need to pay attention to is the type of data you will be putting into the variable. There are three basic types: integer, floating point and string. We know what strings are by now. The other two types are both numbers. Integers are whole numbers, whereas floating point numbers allow fractional parts. Both number types allow two lengths.
The different types of integers effect how big an integer the variable can hold. Bigger means more memory and more processor power used up, avoid them if you can. The same goes for the floating point numbers, but double. Floating point numbers store two parts, the integer part and the fraction part. It also takes much longer for the computer to process floating point numbers than integers. Use integers unless you have to have fractions.
By default, variables are integers. You can make it a long integer or floating point or string with a special code, which is the proper character at the end of the variable name. Variable names can be only letters and numbers in BASIC and must begin with a letter. To make a variable into a string, make the last character in its name a dollar sign ($). We can stick with regular integers and strings for now.
So now, can you write a hello world program using variables and operators? Give it a shot, and if you have trouble just take a look at the answer below.
message$ = "Hello world" print message$
With two more tools on our belt, it's starting to look like we should get a toolbox soon, there's just more tools pouring in! If you feel comfortable with what we covered in this chapter, you should try to combine more variables and more operators to do more things. If you don't feel comfortable re-read it a few times, then try!