Really really basic BASIC
Even if you don't have the foggiest idea how to write or even run a program, then this section is just for you. If QBASIC or other programming language is on your computer, you can easily learn to write software. In a matter of a few minutes, you will actually be writing a program of your own. No kidding. I promise you will have fun and you will not break your computer or feel dumb.
Finding and starting QBASIC on your computer
Before you can write a computer program of your own, you will have to have a computer programming language installed on your computer. There are several languages you could use, but these examples are written in QBASIC.
If you have QBASIC on your computer, your first task is to find it and click on its icon to start the programming language. QBASIC was shipped with most versions of DOS and Windows, but you might have missed it. You can probably load QBASIC from your original Windows CD that was shipped with your computer. You will have to load the original Windows CD, locate the QBASIC files, and copy them to your computer in a folder on your hard disk.
The files to look for on the Windows CD are:
I suggest putting these in the same folder as the Seminar files. If you installed the Seminar from the download, it will be in this location:
C:\Program Files\Ainsworth\Ainsworth Computer Seminar 1
Read all about it...
If you aren't sure how to do all this, I suggest looking on page 62 in Beginning Programming for Dummies. A really neat way to begin learning about computers and programming is with this book by Wallace Wang. Please don't be insulted by my suggesting a book for "dummies." I use dummies books from IDG Books all the time -- and I've been programming for over 20 years.
Check your local bookstore, or order this and many other
books on programming from
To learn more about this book, check the following link
at the Amazon web site.
The QBASIC screen
Writing your first program
Start QBASIC by clicking on the QBASIC icon or running QBASIC.EXE
If there is a dialog box as shown above, first clear the dialog box by pressing the Esc key.
Type the following two lines and press the Enter key after each line:
PRINT "This is my first program."
Press the F5 key to run your program.
The screen shows the output or the results of your program. If all went well, you will see "This is my first program." in white letters at the top of a black screen.
Sounds simple? It is, but giving the computer instructions and having it do exactly what you describe is a very important event. It is this ability to be programmed -- to respond to written instructions -- that makes computers unique and different from all other machines, tools, and instruments. Even though I have written quite a few programs, I'm still excited about the idea that I can use words to turn a computer into something I want it to be -- whether that is a video game, a teaching machine, an assistant to balance my checkbook, or even a seminar about computers and how they work. To me, CLS, PRINT, and other computer words are no less magical than ABRACADABRA!
A word about UPPER CASE LETTERS and spaces in BASIC
You can type words like CLS and PRINT in either lower case (cls, print) or upper case (CLS, PRINT). When you finish each line and press the Enter key or move down with the arrow key, the computer will switch BASIC words to upper case automatically. I like to type all the BASIC words in lower case and let the computer switch them for me. This way, if I make a mistake and type prunt instead of print I will see the typing error right away because the computer won't recognize the word.
If you think your English teacher is critical of your spelling, you have no idea how the computer can be totally upset by a simple typing error. Not only will your computer refuse to accept words that are not spelled exactly right; it won't even let you put spaces wherever you want. For example, the computer won't accept pr int because there is an extra space in the word. Computers are picky that way, but programmers get used to it.
Help and etc.
Press any key to clear the black screen and return to the BASIC screen. Your program should still be there. Move the cursor (blinkie thing) under any one of the letters in CLS and press F1. The BASIC Help system will spring to your rescue and tell you more than you probably want to know about the BASIC command CLS and how it clears the screen. The Help system is always available in BASIC and it almost always tells you stuff you want to know. You can also get Help by clicking on Help in the menu.
By the way, I use the function keys F5 to run a program and F1 for help. These express keys are shortcuts for menu selections. Now that you are a programmer, I will use function keys in these examples because all really hot programmers do that.
PRINT is the other BASIC command you have used. This (guess what) prints stuff on the screen. To learn more than that about PRINT, use F1 and ask your computer to explain it.
Before you leave your first program, you might want to try a simple edit. Change the text between the quotation marks, run your program again, and see the computer print something else on the screen.
Writing your second program
The next two words are very powerful. The words DO and LOOP work together to create program loops. This means that you can request that the computer do something over and over again. You can erase your first program by pressing File and New on the menu, or you can just use the Backspace key to remove the second line.
Now type this program into the computer, but don't run it yet.
PRINT "I'm in a loop"
Before you press F5 and run the program, read this!
When this program runs, it will repeat the PRINT instruction over and over. To get the program to stop, you will have to use the Break key. To do this, press and hold the control key Ctrl while you press the Break key. The Break key on most keyboards also has "Pause" printed on it.
Now press F5 to run the program. After you get tired of seeing it print, press Ctrl-Break to stop it. You can use this method any time to stop a program that is running.
When you stop the program you will see the blue screen again. To see the output of the program, press F4. This shows you what the program was doing the moment you stopped it. To go back and forth between the blue and black screens, just toggle with the F4 key.
Review -- remember these hot keys
To run a program, press F5.
To stop a program NOW, press Control + Break (Ctrl-Break).
To switch between the program screen (blue) and the output screen (black), press F4.
Shortcut -- use the arrow keys
I usually use the arrow keys to move the cursor around on the BASIC screen when I write or edit programs. This is often easier than using the mouse.
Adding a counter
When you add these changes to your program it will count the loops. To add a line, move the cursor to the beginning of any line and press the Enter key. This moves the line down to make room. Your revised program should look like this:
x = x + 1
Now when you run your program it will count each loop and print the total number. You will have to use Ctrl-Break to make it stop. If you add a semicolon (;) after the PRINT statement so that the program reads:
The program will fill the screen with numbers while it counts.
Writing the Electric Poet program
Even if it isn't close to Valentine's Day, this program makes a pretty neat Valentine card. With apologies to Elizabeth Barrett Browning and to any English teachers who may be watching, this is the best computer poetry I can write.
You can enter this program yourself. Before you run your program, read the software carefully and see if you can tell what it will do.
LOCATE 10, 30
PRINT "How do I love thee?"
LOCATE 12, 30
PRINT "Let me count the ways..."
LOCATE 14, 35
n = n + 1
IF INKEY$ <> "" THEN EXIT DO
There are several new BASIC words in this program. First, the LOCATE command tells the computer where to print something on the screen. In this case, the three lines are printed near the center, on lines 10, 12, and 14.
The INKEY$ statement is used as a special escape clause. If any key on the keyboard is pressed while this program is running, the computer exits from the loop and the program stops.
Notice how I use n in this program. This is a variable or simply a letter that represents a number. I could use any letter or word -- such as X, Q, or even George -- to keep track of the number of times this program has printed. Here are some examples to show what I mean.
PRINT "George" 'this prints the word George on the screen.
PRINT George 'this prints the value of George, which is zero.
George = 26 'this sets George equal to 26.
PRINT "George" 'this still prints George on the screen.
PRINT George 'this prints the value of George, which is now 26.
When you print anything inside quotation marks, the computer simply prints what you have enclosed, without "reading" it or paying any attention to what is being printed. When you print anything without quotation marks, the computer evaluates what number this represents, and prints that value. Here are some other examples you can try that will turn your computer into a very expensive pocket calculator.
PRINT 5 'prints the value, which is 5.
PRINT 2 + 3 'this also prints 5, the value of 2 + 3.
PRINT 2 * 3 'this prints 6, the value of 2 times 3.
And so forth...
That's it. You have written an actual program. It worked. You can now drop comments about "the program I wrote" in your conversation and nobody will know the difference. Besides, all programming is just more of the same: making statements to your computer using the special words like CLS and PRINT that the computer understands.
I didn't intend for this to be a complete course in programming. Instead, I wanted you to see how simple programs are written and how larger and more interesting software is made up of many statements telling the computer what to do.
The Programmer's Toolkit is a collection of special tools and techniques that make writing programs a lot easier. When you're ready, look at the Programmer's Toolkit and see what could be the next step for you.
Other links to explore...
Flowcharts -- a computer road map -- introduction to software and the Guessing Game
Exploring time with your computer -- counting and measuring time
Exploring random numbers with your computer -- randomness in games, music, and art