Tutorial for QBASIC/QuickBasic Okay, where to begin programming, if you are a newbie to programming? Well, chances are, if you are an owner of an IBM, you already have a program language availible to you. It's called QBASIC, and it stands for Quick Beginners All-Purpose Systematic Instruction Code. BASIC was invented to teach students proper programming, and it is still used today by several colleges, not to mention people who (gasp!) like programming in QBASIC. If you are thinking that QBASIC is your father's BASIC, then you have a lot of things coming. QBASIC has advanced a long ways from BASIC. First and foremost, no line numbers. For instance: 100 CLS 110 PRINT "Hello World" 120 GOTO 100 Becomes: x=0 DO CLS PRINT "Hello, World" LOOP UNTIL X=1 I'll explain what both of those do later on. QBASIC also contains a lot of simple, English-like commands and keywords that make programming simple and easy. It even has a Windows cousin, Visual Basic, which I'll talk a little about later on. What are the drawbacks? For starters QBASIC is not that powerful. Word Perfect has no need to worry about you; operating systems, word processors other than simple text editors, and DOOM-like games are out of your league. Visual Basic is a little more powerful, but still, trying to create an operating system with either language is like trying to fly a balloon to the moon. Unlike C (another programming language), QBASIC is not compiled. It is known as a _translated_ language, meaning that the program is translated into machine code, which is the computer's native language, and then run in the memory of the computer. It cannot create stand-alone executables; in other words, you have to be in QBASIC in order to run it. QuickBasic, on the other hand, can produce stand-alone exe's, but is rare to come across since Microsoft stopped producing it. In this series, I'll try to explain QBASIC in common English. The first QBASIC book that I got talked about "massaging the data", and I rammed my head into the monitor. In my syntax, this becomes manipulating data, meaning dressing it up, calculating it, and making it presentable at dinner parties. The first program we will do is a makeover of the standard "Hello, World" program. In case you don't know, "Hello World is the most inane, simple program. It is taught in almost every programming language in every class as the first program you write. In this program, we'll learn the following commands: *CLS *INPUT *PRINT *END +LPRINT (extra credit) Simple, huh? "Hello, " Of course, you want your computer to talk nice to you, and talking nice includes being on a first name basis. This program will ask for your name, then print a nice message. We'll start with almost the first command anybody learns, CLS. CLS stands for CLear Screen, and is used to wipe the screen clean to start the program. To start, open up QBASIC, either in a DOS window, or DOS itself, and type on the first line: cls Hit enter, and cls should go to uppercase as CLS. That's the QBASIC editor's way of telling you that CLS is a reserved keyword in QBASIC (we'll go into reserved keywords later). Next step is to add a print statement telling the user that you need their name. So, now you need a PRINT statement. Type the following: PRINT "Who are you?" Don't worry if you didn't type PRINT in upper-case. Like we saw, QBASIC takes care of that on it's own. PRINT prints stuff to the screen. "Stuff" loosely translates into string constants (sentences that never change), values the user types in, values the computer spits out, and so on. So, now we should have CLS PRINT "Who are you?" Now, if you ran it, what would happen is the screen would blank, the phrase "Who are you" would show up on the screen, and the program would end. Now a program that simply asks the user who he or she is and then doesn't stay around to find out isn't that useful, is it? So, we need to get a way to get the input, or what the computer user types. So, type in the following line INPUT name$ INPUT accepts the data from the user, whether it be letters, numbers, or even whole sentences. _name$_ is the variable that stores the name. A _variable_ does just what its name implies; it holds a value that can _vary_ from session to session. The dollar sign on the end tells QBASIC that this is an alphanumeric string. Alphanumeric refers to letters, numbers, and special characters you can't see. Keep in mind this note: alphanumeric variables can store letters and numbers. All other variables can only store numbers. For instance, if I printed the statement INPUT name And I typed "Kalvin", QBASIC would beep at me and tell me what was wrong. "name" is a variable that can only store numbers. "name$" can store letters and numbers. We'll get into other variable types later on. Okay, now we have the program: CLS PRINT "Who are you?" INPUT name$ Now, we have the data, time to spit it back out to the user. In this case, we would use PRINT again, but with a special twist. PRINT "Hello, "; name$; " now go away you're bothering me." Remember when I said that PRINT could print both constants (phrases that cannot change), and variables (things that can change). It can do both at the same time. What you are doing is telling the computer, "Okay, print Hello, get the value of name$ and print it, then print the other string constant". What you have now is the following program CLS PRINT "Who are you?" INPUT name$ PRINT "Hello, "; name$; " now go away you're bothering me." Two things you can do right now. Notice the first print and input statement. Those can be combined into one statement. Input allows a string constant to be placed right before the variable to be inputted. The statement reads as following: INPUT "Who are you? ", name$ This does two things: first, it streamlines your program. While it isn't much in this program, efficency is one of the ways that programmers strive for. Imagine hundreds of lines like this, and you get the point of dividing your code in half. Secondly, and more importantly, it gets rid of the second question mark. If you ran the program before, here's what you probably got: Who are you? ? As your first two lines. The comma in input tells the computer that you don't need or want that space. I hope that you are also noticing the extra spaces I am putting in. These spaces do more than just sit there. They make the program more presentable to the user. What you should have right now is: CLS INPUT "Who are you?", name$ PRINT "Hello, "; name$; " now go away you're bothering me." There's only one more line you need to add, and that's the END statement. END ends the program. Without it, this program will still function, but you still need to add the line. Although it seems silly, good habits are easier picked up the first time around. Later on, the END will be vital, especially when we get into subroutines and branching. The final program should look like this: CLS INPUT "Who are you? ", name$ PRINT "Hello, "; name$; " now go away you're bothering me." END Here's what happened on my machine when I hit the run function. Who are you? Kalvin Hello, Kalvin now go away you're bothering me. A few other things: notice the semicolons within the print statement. Those keep the output on the same line. Without them, This is the output Hello, Kalvin now go away you're bothering me. Other print functions will be discussed later, but for now, remember to hit the semicolon when you want the output on the same line. Also, if you want, change the second PRINT to LPRINT, and see what happens (you should have your printer on when you do this). If you didn't guess, LPRINT sends the output to the printer. Okay, now you should know how to use PRINT, INPUT, CLS, END, and LPRINT to receive data from the computer and put it back, either to the screen or the printer. In the next segment, we'll talk about doing more with the data than echoing it, and we'll also talk about QBASIC itself and basic programming 101. Until then, adieu.