QBASIC Lesson 1 - An introduction BASIC as a language has been around for a long time, and has seen many different versions and variations. Microsoft QBASIC is quite a good version; it has a very good range of features and can support virtually anything that a beginner could want to do, albeit slowly. Learning it will be a useful step on the road to becoming a proficient programmer. Before we delve into the language itself, it is important that you understand the basics of the QBASIC editor. The main window, the editing window, is where you type your program. Pressing Shift+F5 will run this program, or you can select run from the Run menu. There is a small window at the bottom of the screen called the immediate window, where anything that you type will be immediately run. Along the top menu are some useful menu icons, most useful of all will be "help" - this contains information on how to use the editor and also an index of commands which is immensly useful, although some of the definitions can be a bit cryptic. Feel free to explore through the help files, there is a lot of useful information. Okay, I think it's about time for our first program. At this point, by the way, it will be very useful to have this tutorial open in one window and QBASIC open in another. If not, you're going to spend a lot of time switching between the two. Okay, click at the top of the editing window so that the cursor appears and you can type in words. Put CAPSLOCK off - you'll find out why later, but trust me, it is important. Now, enter: print "Hello World!" and press return. You will notice that the word 'print' instantly changes from lower case to upper case - this is the computer's way of showing that this is a command that it understands. Any guesses at what this program will do? Press Shift+F5 to find out. Original or what? Okay, so it may not be Doom, but it's a program. Press any key to get back to your editor, and have a quick think about what happened. The computer starts at the top of the file and works down. The first thing it comes to is "PRINT", which means "Place on the screen the following:". It then comes to the string (string means stored text) "Hello World", takes this as per instruction and prints it out on the screen. Easy. You may be thinking that a program that prints "Hello World" every time you run it is a bit useless, and you'd be right. A computer is supposed to do things that would be difficult for a person to do, and you could do exactly the same thing with a piece of paper and a pen. That's where variables and strings come in. Variables are the most important thing that a computer deals with. A variable is simply a stored number that can be printed, added, subtracted, multiplied, drawn, in fact anything at all that you do on a computer involves variables. When you are playing Doom 2, your health is stored in a variable. Whenever you are hit, a command decreases the variable. When you are playing Quake, you position is stored in variables. Whenever you move, these variables are changes. So how do we use variables in QBAISC? Easy. Simply type in any word that the computer doesn't understand (not in speech marks though, otherwise it will be treated as text) and it will treat it as a variable. Here is an example: cabbages=1 cabbages=cabbages+1 Most programmers, however, don't like the idea of using massively long variable names, mainly because it takes ages to type in. Okay, big shock now: we're going to write a useful program. Not a stunningly useful program, I admit, but a useful one. We're going to write a program to ask you for two numbers and print out the result of multiplying the two together. I think the best way of doing it is to give you the program so that you can type it in and see that it works, then I'll go through it a line at a time: cls input "Please enter the first number",a input "Please enter the second number",b r=a*b print "The answer is";r Okay, now here's the line-by-line breakdown: cls This is a very useful command that is also very easy to use - just put it in and it CLears the Screen. input "Please enter the first number",a The input command is used to let the user type in letters or numbers. In this case it's asking for a number, which will be placed in the variable a. So, it will print "Please enter the first number? ", wait for you to type a number then put this number in the variable a. input "Please enter the second number",b This input command works in the same way, but it prints a different message and the number that you type ends up in b. r=a*b This is the command that does the work. It makes r equal a time b, in other words: work out what a*b is and put it in a. print "The answer is";r This is an interesting one. You see, the print command can also print out variables, and using this command is equivalent to saying: Put "The answer is" on the screen followed immediately by the value of r. The semicolon tells the computer not to go onto the next line before printing the value of r; try it with no semicolon, or with a comma there instead. It will look different each time. That should about wrap it up for this tutorial. Experiment with the different programs and commands; there is nothing that you can do that will damage your computer. Look in the index of commands for further information on any of the commands I have talked about. I'll leave you to it.