Typing Programs
You're reading this book either because you want to learn to program or because somebody is reading your copy of People magazine. In any case, to write QBasic programs, you have to learn to use QBasic's editor. This section teaches you just that.
To start a new program, you must have an empty edit window. To open one, select the New entry of the File menu; this opens an empty window named Untitled. It is into this window that you'll type the commands that make up your QBasic program. Before typing a new program, however, you should give the empty window an appropriate name. (Names like Adrian, Samantha, and Guido the Man are out.) To name your program, first select the Save As item of the File menu. You'll then see a dialog box similar to the one in Figure 3.5.
Figure 3.5 The Save As dialog box.
When the Save As dialog box appears, type the filename PROG1.BAS into the File Name edit box. Then press Enter on your keyboard or click the dialog's OK button with your mouse. When you do, the name of the window changes to PROG1.BAS.
Point of Interest
All your BASIC programs should use the file extension .BAS (short for BASICget it?) because that's the extension QBasic expects programs to have. Moreover, that's the extension used by other programmers.
Now that you have a newly named program window, you can type your program. Type the word cls (note the lowercase) and press Enter. What happened? If you typed the letters correctly, QBasic changed them from lowercase to uppercase. This is because CLS is a QBasic keyword, a word that is part of the QBasic language. (For those who are curious, CLS is the command to clear the screen.) Whenever QBasic recognizes a keyword, it automatically makes it all capital letters.
Programmer Lingo
A keyword is a word that is part of a programming language. Keywords, also known as reserved words, cannot be used as anything else in a program.
Now type the following text line exactly as it is shown herebe sure you include all punctuation (and the misspelled words!):
input Whatis yourr name"; Name$
When you press Enter, you see the dialog box in Figure 3.6.
This is a syntax error dialog boxQBasic's way of telling you that you're not as smart as you think you are. Just as with human languages such as English, computer languages have rules that dictate how sentences (or, in this case, commands) must be constructed. In the line you just typed, you didn't follow the rules, so QBasic complained.
To fix the line so that QBasic can understand it, you must add a quotation mark immediately before the word "What":
-
Press Enter to get rid of the syntax error dialog box.
-
Use the left-arrow key to move the flashing text cursor under the letter W in the word "What."
-
Type a quotation mark (") and press the down-arrow key to move from the line.
Figure 3.6 QBasic showing a syntax error.
Now QBasic recognizes the command. In addition, it changed the word "input" to all capital letters because INPUT, like CLS, is a QBasic keyword. However, just because QBasic can now make sense of the line doesn't mean the line is correct. It only means that the line follows QBasic's syntax rules. You still have two misspelled words: "Whatis" and "yourr."
QBasic's text editor enables you to correct typing mistakes easily. First, use the arrow keys to position the flashing text cursor under the space between the words "yourr" and "name." Now press the Backspace key. Presto! The extra letter vanishes, and the rest of the line moves to the left to fill in the space you deleted.
You still have a mistake in the line, though. There should be a space between the words "What" and "is." Luckily, you can add characters to a line as easily as you can delete them. Use the arrow keys to position the blinking text cursor under the "i" in the word "Whatis" and then press the spacebar. When you do, a space character appears at the text cursor's position, and the rest of the line moves to the right.
Finish typing the program by adding the following lines. Remember to type them exactly as they're shown. To indent the line that starts with the word PRINT, start by typing two spaces.
FOR X = 1 TO 200 PRINT Name$; " "; NEXT X
Your screen should now look like Figure 3.7.
Figure 3.7 QBasic and your new program.
Soon you'll run the program and see what it does. First, however, you should save your program to disk so that if anything goes wrong when you run it, you won't lose your work. (It's possible for a programming error to lock up your computer, forcing you to restart it. It's also possible for a programming error to infuriate you enough to throw your computer out the nearest window. In either case, you'll be glad you saved your program first.)
To save your program, select the Save item of the File menu. After you select this command, your program is safely stored on your disk under the name PROG1.BAS (or whatever you named the program).
To run the program, select the Start command from the Run menu or press Shift+F5. This program first asks you to enter your name. Type your name and press Enter. You should then see a screen similar to the one in Figure 3.8.
To get back to QBasic, press any keyas it says at the bottom of the screen.
Congratulations! You've just written and run your first QBasic program. Pat yourself on the back, go get a snack, and brag until everyone in your household is thoroughly annoyed.
Figure 3.8 Your running program.