KeyWords: END, LET, PRINT
To fully utilize the QBASIC system with all the options makes it quite a complex command. Here is the command in full:
QBASIC /B /G /H /MBF /NOHI /RUN [filename]
Quite a command, huh? I know you're thinking "How in the world am I gonna remember all that?" Well, do we have some good news for you! We will now tell you the slightly easier way to start QBASIC. Simply type in:
and press Enter. Voila! You are now in QBASIC! See? Wasn't that easy? Oh, by the way, we won't tell you what all that other seemingly unnecessary stuff is for the QBASIC command. We will just say that it is for telling the system how to handle different video configurations. By the way the Q in QBASIC stands for QUICK. QBASIC was derived from Microsoft's highly successful and incredibly powerful QUICKBASIC compiler series. QUICKBASIC has many more capabilities than QBASIC, and allows much larger programs to be written, including easier interfacing with other high-level languages (such as C++) and low-level languages (like MACRO ASSEMBLER). I have even combined all three of those languages into a single program using QUICKBASIC. It can't be done with QBASIC. Oh, well. QBASIC doesn't cost anything, so I really can't complain!
Let's take a look at what is on the screen. At the top, you have a line of words. This is the menu system. We will be using it very shortly. Most of the rest of the screen is blue, except for that white box in the middle that says Welcome to MS-DOS QBasic, and gives the copyright notice. Press the ESC key to clear this box. You will see that the screen is divided into two sections. The top section has a title above it that says Untitled. This is where the program name will appear when you are working on a program. The bottom window has a title of Immediate. We will be using this section when we write and test programs. The very bottom line shows a handfull of function key shortcuts, and your cursor position.
Enough chatter, let's get this computer to do something interesting!!!
Yes, it's time already to learn our first command!!! What is it, you ask?
Well, it' the (drum roll, please....) PRINT command!! Can you guess what
it does? Well, let's try it and see. First, press the F6 key until the
word Immediate (near the bottom of the screen) is highlighted. That will
allow us to type in QBASIC commands directly.
Type in the word
(it can be in upper or lower case - the computer doesn't really care) and press the Enter key. Oops! It looks like we are viewing the screen that we had when we entered our QBASIC command! In fact, we can still see the command we entered, with a line at the bottom that says Press any key to continue. Go ahead and press any key, and you will be back in the QBASIC environment. Let's get this thing to do something a little more fun. Now type
and press ENTER. Notice that there is now a number 4 at the bottom of the output screen? That's because we used the print command to print the number 4.
Let's recap. You told Mr. Computer to print 4. What did it do? It printed the number 4 on the output screen! Wait, it gets better. Now type this:
Press the Enter key. Did you see that? Now it printed the number 7 on the output screen! Isn't this great? Let's see if we can print words, too! Type this:
and press ENTER. Wait a minute! It didn't print the word lunch, it printed a 0! This thing must be broken! Why didn't it print out what I told it to do? Hold on - how did we that it printed a 0 instead of the word lunch? Do you think maybe it's supposed to do that? The answer, of course, is yes. In the above example, the word "LUNCH" is what is known as a numeric variable. That simply means that it is a name which can represent just about any numeric value. You could call it a storage place for data. For example, we could assign the word LUNCH to have a value of 10. How do we do that in QBASIC? That is our second command we will learn. It is the LET command, and it looks like this for our particular case (go ahead and type this in ):
LET LUNCH = 10
and then press the ENTER key. Hmmmm, It didn't show us the output screen this time. That's good! That means that QBASIC understood the command that we typed in, and acted upon it. Now type in:
Look at that! It printed out the number 10! Let's try to change the value of lunch. Type:
LET LUNCH = 49
No response - good. Now type
You should get a 49 on the output screen. Neat, huh? We can use the same variable name to represent different things! Of course, when we told LUNCH to store the value of 49, it threw out the previous value of 10. A variable can only hold one thing at a time. If you tell it to hold something new, it has to let go of what it was holding before. It only has one hand, you know!
Now we start to cook. Here we are going to use two numeric variables, and a math function. Type in this series of lines (press Enter after each line):
LET FIRST = 9 LET SECOND = 6 LET ANSWER = FIRST + SECOND PRINT ANSWER
Did you get what you expected? If everything went correctly, there should be a 15 on the output screen.
Let's explore what just happened. The first two lines you should be familiar with. The variable FIRST was assigned a value of 9. The variable SECOND was given a value of 6. The variable ANSWER was then given the value of SECOND added to the value of FIRST (6+9 in this case). Then the value of ANSWER was printed. That third line is where the power of computer programming begins to become apparent. You can put this line in a program, and it will add whatever those two variables (FIRST and SECOND) happen to be at the time. Let's look a little closer at the third line. QBASIC operates on the LET command by solving any math problems that is to the right of the equal sign using the standard Algebraic Order of Operations. If pain persists, see your mathematician. It simplifies this expression down to a single value, and (assuming no errors like dividing by zero) assigns it to the variable on the left side of the equal sign. There can only be one variable on the left side of the "equation", and no math operators. It can only be a single variable. However, you can have a highly complex formula on the right side of the equation. As long as it can be reduced (or solved) to a single number, anything is fair game on the right side of the equal sign!
Now let's get right into some real programming. All we have been doing so far is using QBASIC's "immediate mode". What that means is QBASIC performs the command we type in immediately after we press Enter. The other mode of QBASIC is (can you believe it?) the "Program mode". In the program mode, the instructions or commands we type in are not performed as we enter them, but instead are stored in the computer's memory as a program. After we get the program entered into the memory, we can then execute the program at a high rate of speed. How do we get into the program mode? Press the F6 key until the word Untitled (at the top of the screen) is highlighted. You will also see the cursor (that flashing underline thing) jump up to the program window. Now type this in:
LET FIRST = 14 LET SECOND = 8 LET THIRD = FIRST + 6 LET ANSWER = FIRST + SECOND + THIRD PRINT ANSWER END
Let's go through this line by line. Lines 1, 2, and 5, you have seen before. However, the third line looks a little different. It is straightforward, however. It adds 6 to the current value of FIRST (14 in this case) and assigns it to the variable THIRD. The fourth line simply adds the values of all three variables and assigns it to the variable ANSWER. Incidentally, all the variables that are on the right side of the equal sign in a LET command are not altered. In other words, the FIRST, SECOND, and THIRD in line 4 do not change or lose their value when the line is executed. The 6 in the third line is called a constant. That is because its value doesn't change when you run the program. QBASIC allows you to define words as constants so that the language become more symbolic. For example you could just type in PI instead of 3.14159 every time you needed that value. We'll show you how in the Advanced series.
The last line contains a new command, END. It obviously is the end of the program. If there happens to be more program lines after the END command, they are not executed, because END tells the computer to stop. Simple, really. Now we need to tell the computer that we want this program to run. How do we do that? Take a look at the very bottom of the screen. Notice that it tells us that the F5 key is a shortcut key to run the program. Go ahead and press F5 to run it. Oh, look! You get a 42 as a result. Can you figure out why? Right! FIRST is 14, SECOND is 8, and THIRD is 20 (14+6). Add the three up, and you get 42. Now it is your turn to write a few programs. If you want to do more than add, here are a few more operators:
Addition is "+"
Subtraction is "-"
Multiplication is "*"
Division is "/"
To erase the existing program so that you can start with a new one, select
the File menu either with the mouse (by clicking on it) or the keyboard
(by pressing Alt-F). Then from the pull-down menu, select New either with
the mouse (by clicking on it) or the keyboard (by pressing N). You will
get a box asking if you want to save the changes. We really don't need
to save this, so answer No. You will then be presented with a clean slate.
Introduced In This Chapter:
Keywords: END, LET, PRINT
Concepts: Constants, Numeric Variables.