Computer Programming




Ever wondered how a computer program works or what it takes to make one work? Well, this is your chance to find out and it won't cost you anything, initially, if you're on a PC compatible using Windows 95 or 98 (and not NT, 2000 or XP). Most PCs come with a form of BASIC (Beginners All-purpose Symbolic Instruction Code) or you can download a shareware trial copy of First BASIC for free! First, see if you have a copy of QBASIC already on your system. Go to the start menu, select FIND FILES and search for QBASIC. If nothing comes up you'll have to down load First BASIC, which is quite similar. You can find this program at: http://www.powerbasic.com/files/pub/

Understand that neither QBASIC nor First BASIC will give a program that looks or acts like the programs you are used to working with, unless you come from the older DOS era of computers (pre 1990). In other words it's an ancient system, but it's free and will show you the humble beginnings of programming, so that if you like it you can buy a copy of the more modern language, Visual BASIC, new or used from E-bay (and for as little as $20 in an older edition, which will suffice for the beginner).

There are two formats of computer programming: Command line (which is what First BASIC and QBASIC are all about) using typed in text commands (the way we used to run programs from the 1950s to the 1980s) and Object Oriented Programming (OOPs). Visual BASIC is an object oriented language. This browser you are using is an object. It is self-contained and Visual BASIC 6.0 Professional actually comes with a fully functional browser object you can put into your programs. The browser is also made up of separate, individual objects, such as push buttons and drop down menus. These objects are ready made in an OOPs language like Visual BASIC and all you need do to use them is drag them over to a program with your mouse and then link them to other objects and functions. The icon on your screen for Explorer or Netscape, for example, is another object and when you click on this it links to the browser program and runs or executes this program. That's how a link works. It looks for an action from the user (called an "event") such as a mouse click. When it senses a mouse click the programmers have provided instructions for some "action" to occur -- in this instance starting up the browser program.

Since our free BASICs are not object oriented you must use text and graphics to create a portal (the interface or "front end") for interaction between the program and the end user. Since this portal is not pre-built, like a browser object in VB (short for Visual Basic) you have to build this world yourself on the computer screen using the programming language (and just for the record, objects in VB are built this way, also, from scratch using programming code, it is just that in VB they give you a collection of pre-fabricated gizmos to play with that are not found in the free, command line languages).

The actual programming language for both command line and object language is almost the identical. The difference is that the command line version works with DOS and the newer VB system works with Windows. Let's take a moment to look at the operating system:

DOS was the original operating system (or OS) for the IBM compatible PC and it evolved through two earlier systems: UNIX and CP/M. Unix was the original serious computer language developed by AT&T in the 1960s for networking and is used by most colleges, many businesses and most of the Internet as the primary operating system (Linux is an offshoot of UNIX). CP/M was a similar but more simplified operating system for the first home computers. Both use typed in commands to do functions. You type these commands in next to a flashing cursor or question mark called a prompt. You still see elements of this today when you start up Windows 98 or ME. All that text against the black background is DOS commands pre-written by Microsoft that make the system work and then automatically launch Windows. DOS still sits under Windows, except on NT, which is why you can't use the free First BASIC program. Windows NT has no DOS. In the near future all Windows (starting with the new XP) will be DOS-less, but currently there are so many programs that rely on DOS that Microsoft has not yet weaned us of this system that is barely 20 years old!

Everyone here should be old enough to remember arcade games such as Doom. It was all the rage just a few years back. Doom is a DOS game. It's written in DOS and runs in DOS mode. This is because until recently DOS was much faster to use and arcade games require higher operating speeds. Because people are still playing DOS arcade games and even using some DOS applications (one person I know still uses Form Filler which is written in DOS) they haven't killed off DOS, but it will be gone by 2005.

Windows runs over DOS and works with DOS to do some functions, which is why DOS is faster. Windows must pass information to DOS, which then does these functions, returns information and passes that back to Windows. This process requires extra steps which slow down the works!

IBM OS/2 and Windows NT have done away with the DOS process all together. OS/2 has a DOS emulator so you can run some DOS games. NT does not have any DOS or emulator at all. Both NT and OS/2 are more stable than Windows because they are not DOS based. This is why we will eventually be DOS-less, but Microsoft is still sensitive to the people out there using DOS programs, such as Doom or Form Filler.

So if you want to experiment call up the QBASIC or download and run First BASIC (also called PBASIC 2.1). It will come up in a blue window or be full screen. You are now in DOS mode and the screen size is that of VGA (640 x 400 pixels). Your mouse will not work in First BASIC (this is normal) so you will have to navigate with the TAB and arrow keys on your computer. If you find programming is fun or interesting, then you can quickly upgrade to VB or a newer version of PBASIC that works with the mouse. For now, we are making due with a free taste.

In future installments we will show you the code in both QB (QBASIC or PBASIC or First BASIC) and VB (Visual BASIC) so you can understand how both command line and object based languages work.

The first thing you will see when you run QB or PB or FB is a "Welcome" sign. Press the Escape key (upper left on your keyboard called "Esc" and in this text we will signify all keyboard keys with brackets like these:[ ] and this key would be [Esc]) to get rid of this. You should now see a bright blue screen with a cursor rectangle at the top. That is your prompt. As you press letters on the keyboard they will appear at this prompt and move to the right.

This, then, is your programming "world" which is sometimes called a "virtual workstation" or "desktop" and equates to the plain background you see on Windows, except there are no little Icons like "My Brief Case" or "My Computer" which are Windows objects. There are no objects in a program such as QB. It is possible to make some objects, but you have to do it from scratch, including a full set of instructions for making them do things. Why is this? Windows has these actions and events pre-programmed (which is another reason why is runs so slowly). DOS and QB do not have these pre-fabricated and ready to go! It is a very rudimentary and "dumb" programming world in which anything fancy and "smart" must be built from scratch by you, the programmer!



Advertisement


In VB (Visual BASIC) you see the typical program with menus and tool boxes. In the middle of the screen is a square called a form and that would be your "world" in VB. It is not a desktop or virtual workstation, but an object that sits over the desktop. That is the best way to look at it: Visual BASIC deals in Objects that are placed over your world. You build other objects into that main object and then interact with these objects to create a program.

Older QB is the world and you fill that world with text and graphics to create a program. This means there is more coding in the older, DOS based world than in the newer, object based Windows world! But the code used is essentially the same. All the methods for manipulating information is virtually identical and you can actually put most QB code directly into a VB object and then run the old QB code so long as it doesn't contain obsolete elements!

Again, we are only dealing with QB, FB and PB because it can be obtained for low or no cost. Once you are sure programming is something you want to invest some money into, feel free to buy a copy of VB 4, 5 or 6.

As a point of interest. QB makes use of your mouse if you have a DOS mouse driver active and installed. So does the newer versions of Power BASIC (PB) that you must buy. But the shareware First BASIC (FB) does not support the mouse, however you can create your own programs that use the mouse in FB (provided you have a DOS mouse driver) or PB but you cannot create mouse programs that easily in QB. This is because QB has no direct access to the DOS tool set (known as DOS interrupts) for which FB and PB do have special procedures for interaction with the DOS operating system tools.

Now we program:

Because in QB we use the entire desktop or world, we need to do regular housekeeping. The primary tool for doing this is the command to clear the screen: CLS

Programming words can be upper or lower case so it can be either: cls or CLS or even Cls.

What CLS does is basically activate the line feed command of DOS which scrolls our virtual world by 26 lines. So you always make the first line of your program CLS to clear the virtual world of any text.

The next thing we need to understand is the process of getting and giving information. We get information from the person using our program (called an end user) with the INPUT command. The INPUT command also allows us to display a line of text which can be used for instructions, ending in a question mark (which is our prompt). There for, like the TV show, Jeopardy, all prompt commands should be formulated as questions:

CLS
INPUT "What is your first number"; A
INPUT "What is your second number";B
PRINT "The answer is...."
PRINT A + B
END

Let's examine the INPUT line: INPUT tells the computer that we are going to get some information from the end user. This information will be stored in a universal container we call a variable and a variable can be any combination of letters so long as they are not a word reserved to the programming language (in other words you can't make INPUT a variable, but you can make INPU a variable). In this case we kept it simple and made the first variable A and the second one B. In-between and inside the quotes is our interactions to the end user. They must be enclosed in quotation marks and are generally separated from the variable using a semicolon (;). The use of INPUT A and INPUT B is also allowed, but the prompt would only show as a question mark:

?

The user wouldn't know what to do. Here it shows up like this:

What is your first number?

Anyone with computer experience should know that they are expected to type in a value, however you can amplify this statement as you desire to make sure the user knows what to do. In the old days we used to add phrases like: Type in a value and press the key. Making the line look like this:

What is your first number (type in a value and press )?

So if you type in the number 6 our program would look like this:

What is your first number?6
What is your second number?

If we then type in 15 it would look like this:

What is your first number?6
What is your second number?15
The answer is...
21

This shows us how we get information back to the user, via the word: PRINT which will print the value of any variable or set of math equations directly, which is what was done here! It will also PRINT any text enclosed in quotation marks, such as the line: "The answer is..."

So you can:

PRINT A
PRINT B
PRINT "The above when added together equal..."
PRINT A + B

The first line would print 6, the next line 15, the third line would print the phrase: The above when added together equal... And the final line would print the end result of the math of adding variable A to variable B (6 + 15).

You can also combine text and math or text and numbers, like this:

PRINT A; " plus " ; B ; " is equal to: " ; A+B

This line prints out as:

6 plus 15 is equal to: 21

Try the whole thing, together:

CLS
INPUT "Your first number is"; A
INPUT "Your second number is";B
PRINT ""
PRINT A; " plus " ; B ; " is equal to: " ; A+B
PRINT A; " + " ; B; " = "; A + B
END

This, using the same values (and any values will work) would be:

Your first number is?6
Your second number is?15

6 plus 15 is equal to: 21
6 + 15 = 21

We use an empty PRINT statement to generate blank line (housekeeping) and we showed two different ways of printing out the results.

A final way to do the finished program:




CLS
INPUT "Your first number is"; A
CLS
INPUT "Your second number is";B
CLS
PRINT A; " + " ; B; " = "; A + B
END


This would clear the screen after obtaining all the information (data) and display only the problem and answer:

6 + 15 = 21

The END statement is a formality that is optional, but should be used just to make sure you and the machine know that the program is now ended.

So we have learned to clean up with CLS (CLear Screen) and to get data with INPUT and to show data with PRINT. We know you can assign numbers to letters (variables) and print these to the screen. We can print text enclosed with quotations using either INPUT or PRINT. We can print blank lines by using empty quotes "" and we can add to numbers or variables together by simply putting the PLUS sign (+) between them:

PRINT 6+15
PRINT A+B

So you get information into the program from the user with INPUT and this must have a variable next to it after a space: INPUT variable or INPUT A or INPUT b or INPUT dekfxws. All of these are generally legal. The simpler the better as you may have to write this out dozens of time. If you want to add an explanation or instruction it must be enclosed in "quotes" and should be followed by a semicolon (;) and then the variable: INPUT "Quantity" ; Q.

You can print out anything enclosed in quotes with the PRINT statement: PRINT "This line here!" You can also print any variable: PRINT A. You can PRINT several variables by separating them with either semicolons (;) or commas (,). A comma adds a TAB space (generally 10 spaces) between values. A semi-colon adds no space:

PRINT A;B
PRINT A,B

The results for this would be:

615

615


You can combine text inside quotations and variables or values but they must be separated by semicolons or commas:

PRINT "Value A is "; A; " Value B is ";B

Remember to leave extra space inside the quoted area if you want to separate the value from the text:

Print "Value A is:";A
Print "Value A is: ";A

Would come out like this:

Value A is:6
Value A is: 6

You can add numbers or variables directly and print the results: A + B or 6+21 (and as suspected you can also subtract, multiply and divide: 6-21, 6*21 and 6/21, as a dash [ - ] is subtraction, an asterisk [ * ]multiplication and a slash [ / ] division).

It's good to terminate your program with END, but not mandatory. It's good housekeeping and the program will behave better.

To do this same sample program in objects using VB (see photo):

Select NEW PROJECT from the FILE menu. A small form appears (number 4). From the tool box select the white rectangle (2) near the top that has the letters “ab” inside. Drag one of these over to the form and position it top left and size it with your mouse.. Drag a second one over and position it top center and make it the same size.. Go to the big letter “A” (1) and drag that between the two text boxes. Get a second label from the big “A” and drag it to the far right. Go to the gray rectangle or push button (3) and drag it and place it between the center text box and the far right label.



Now put your mouse on the top left area of the form, click and hold the left mouse button and drag it over all these objects then release the button. They all should highlight. Click your right mouse button over any of the objects and select the Properties option. A Properties box will come up (6). Select FONT and then select 14 points and bold. All the objects will now have 14 point bold text. Go to the bottom of the form and click the left mouse button to remove the highlighting.

Your list of objects should be:

Text1
Text2
Label1
Label2
Command1

Going from left to right with right mouse button clicks, position the mouse pointer over each object. For the Text1 and 2 select the Text option inside properties and remove the text that is there (Text1 for example). For label1 select the Caption property and change this from LABEL1 to the plus sign: + For Label2 clear out the name Label2 from the Caption property. For the Command1 (the push button) change the caption from: Command1 to the equals sign: =.

Double click the push button (command one) and the program code box will appear (5), put this line of text inside the Click event (upper right scroll bar, which should be default when you click on it):

Label2.Caption = Val(Text1.Text) + Val(Text2.Text)

It should now read:

Private Sub Command1_Click()
Label2.Caption = Val(Text1.Text) + Val(Text2.Text)
End Sub


That’s it. You’re done. Select START from the RUN menu and then enter two numbers into the text boxes and press the push button. The answer should appear at the far right.

It should be noted that Labels (the object in the tool bar denoted by the big letter "A") have a lesser amount of Object functions than Text boxes (the white square denoted on the tool bar with the small letters "ab") and thus take up less room in your program and increase operating speed. You want to use a Label when displaying information and a Text Box in situations where the user needs to add information manually, which a label can't do directly.

We also had to learn an advanced Basic keyword for our VB program: VAL(). Val() is a function that converts text based numbers into real numbers. As the term Text Box denotes, all items contained in such an object, including numbers, are considered to be "text" and are thus held in memory as text. Adding text together turns them into a compound result: 5.25 + 10.03 as text would be displayed as: 5.2510.03 which is not what we want to do. Instead we need to convert these text characters into numbers that can be added, subtracted, multiplied and divided rendering a new math value. QB also support the VAL() keyword, which stands for "numeric VALue of the first number text characters found in a sentence or line."

If you have any problems with either of these programs write us at: basic@issues-mag.com

Tell us what type of computer and BASIC you are using and what problems occurred. We will get back to you as quickly as possible!



 






The Musician's PlaceTo Shop!
Instant Gift Certificates!














© 2001-2005 Issues Magazine.
All Rights Reserved.
editors@issues-mag.com




Get 15 FREE prints!