Thank you for helping keep Tek-Tips Forums free from inappropriate posts. The Tek-Tips staff will check this out and take appropriate action. Click Here to join Tek-Tips and talk with other members! Already a Member?
Join your peers on the Internet's largest technical computer professional community. It's easy to join and it's free. Register now while it's still free! Already a member? You don't need to use my name, put yours in there instead "Jack was here ", or whatever. Now run it. Ok, that's great. Maybe it should have been called the "brake" key. On laptop keyboards, the break key might be a little harder to figure out.
Sometimes you have to press the "Fn" key followed by the key that says break. Sometimes it is color-coded to help you figure it out. You'll know you have it when the program stops and the editor comes back up. This is called "indenting" and it makes the program easier to read. You can see the beginning and the end of the DO LOOP more clearly. It's important to make your programs look nice so other people can read them.
So far our programs have only talked to us. Let's write one that will listen. Try this:. To be variable means that things can change.
Sure enough, the message changes. Variables hold letters and numbers. These are called "string variables". Variables without a dollar sign can only hold numbers. We'll be seeing them soon. You can call your variables anything you want. What happens when you run it? Another way to think of a variable is to imagine a small bucket with a name on it. This is the bucket's variable's name.
Now take a piece of paper and write your name on it and drop it into the imaginary bucket. Computer variables can only hold one piece of paper one value at a time.
This program will show you how this works:. Let's make that last program a little smarter. I want to be able to identify intruders playing with my programs.
Wouldn't it be great if the computer could recognize someone's name and print a special message for them? How about this:. Run the program and type in your name hopefully it isn't Mike.
You should see the same old message as before. Now run it again and try entering "Mike" or "Joe" or whatever you changed it to. If it didn't tell the right person to go away, make sure you typed the name correctly. Make sure you enter the name exactly the way you put it in the program. The "IF.. THEN is called the "condition". With numbers you can also check for "greater than" and "less than":. Another option is "not equal" which can be done like this: "Number ".
Using IF THEN can make your programs very interesting. It lets your program make decisions and do something appropriate. Computers are very good at math. Let's get the computer to do some math for us. Here's a simple multiplication calculator:. Run it, and enter two numbers. It does an excellent job multiplying for you. This is because they are only holding numbers, not letters.
Because there's no division sign key on the keyboard. At least I haven't found one. The first thing we need to think about is where we are going to save our programs. The two main places we can save things are the hard disk, and onto a floppy disk. Let's look at both places. Using a floppy is a good idea if you don't want other people looking at your programs. The floppy can be removed from the computer and kept in a safe place.
Floppies are also good when you want to move programs from one computer to another. Let's try it. Find a blank floppy and place it into the computer's floppy drive. The floppy can have stuff on it, as long as there is some free space left. You'll need to have a program up on the screen for this to work properly. Go back to the previous chapter and type in the example if you need to get something up on the screen.
The "Save" dialog will appear on the screen. Notice that the cursor is in a box field called "File Name:". Type this:. What happened? Did you hear something? Where'd the "Save" dialog go? Hmmmm, ok, hopefully that worked. How can we check? Wait, it's already there. Ok, then let's clear it out and try to get it back from the floppy. Remember how to get rid of a program? Hint: "File" then "New" on the menu. Let's do it. Click on "File" then "Open Now you will see the "Open" dialog.
BAS" in the box called "Files". Two clicks oughta do it. BAS" and the program is back from the floppy. The hard disk is the most convenient place to save programs. Since it is always in the computer, your programs are always there for you to load when you need them.
Hard Disks usually have a lot more information on them than a floppy. Because of this, you need to be concerned about keeping things neat. You'll never be able to find what you need otherwise. The best way to organize your collection of programs is to put them into a "subdirectory" or "folder" on the hard disk. Let's make a folder on the hard disk for our programs. In Windows 95 we will need to use Windows Explorer. In Windows 3. First, the Windows 95 way. Go ahead and type in a name for the new folder, but keep it short!
I called mine "TedsPrgs" meaning Ted's Programs. Good, that's done. We won't need to do that again, unless you'd like to make another directory at a later date.
Saving to the hard disk is only a little different from saving to a floppy. You'll need to have a program up on the screen to save. The cursor is in a box field called "File Name:".
Now you will see a list of programs in that directory. Since you haven't saved anything yet, there shouldn't be any programs there. Go ahead and type this:. Hmmm, no sound at all that time. Hard disks are much more quiet than floppies. So, let's make sure it worked properly. First, get rid of what's on the screen with a "File" then a "New". This is also very similar to the way we loaded a program from floppy. This will bring up the "Open" dialog. BAS" in the "Files" box.
BAS" to load it. That wasn't too bad, was it? Once you figure out whether you want to save to floppy or hard disk, you only need to remember one way to save and load.
ELSE is fine if you only have two things you want to check. What if you have 5 or 6 friends that might use your computer and you want the computer to say something different to each of them? Whew, that was a big one. Fortunately we learned how to save in Chapter 8. Save it if you want before running it. Feel free to change "Ted" and "Mike" to "Laura" and "Robin" or whoever. So far, we've only let the user fill in our variables. We can fill in variables on our own inside our programs too.
Like this:. You get the picture. Then the program prints them out to prove to you that they are there. See if you can figure out what this rather clever program will do.
Then type it in and run it to see if you were right. Did you get it right? Did the output go by way too fast? You'll have to press Break to stop it. Then take a closer look and see if you can see what's going on.
Random numbers are numbers that you can't predict. Flipping a coin or rolling dice will give you a random number. Random numbers are very important in games and in some kinds of Math. Computers can generate random numbers pretty well. RND is a special function that gives us a random number between 0 and 1. We can use this in games to make things interesting. RND is perfect for rolling dice or flipping a coin.
First let's see RND in action:. This program will print RND twice. Notice that you'll get two numbers that appear to be unpredictable and random. But, try running the program again. You'll get the same "random" numbers. This means your games would always be the same each time the user runs them.
Fortunately, there's a way to fix this. Random numbers between 0 and 1 aren't really very useful. What you will need for a game might be a random number between 1 and 6, like when you roll dice.
To get something more useful, we'll use math. Fortunately, computers are very good at math. There are two problems we must solve to get the results we want.
First, the range of random numbers has to be expanded from 0 through 1 to 1 through 6. That's easily done like this:. Very Good. Not bad. Nice Try. No Comment. Fix it. Qbasic Home. Catalog categories. Qbasic Tutorial [31]. Rate my Site. Main » Articles » Qbasic Tutorial.
Views: Rating: 0. Qbasic Clock. Now you'll have four options: 1. Run QBasic 1. Run QuickBasic 4. Run Quickbasic Extended 7. This package comes with a number of demonstration and utility programs written in Basic.
0コメント