Learn Qbasic!Come on over to Qbasic New's tutorials section which is filled with tutorials covering every qbasic command!Qbasic ForumsCheck out the Qbasic News Forums! Hang out with people who like QBasic, just like you! Must-see: Another World Memory Game A combination of two classic games, albeit two different games. If you are a fan of Another World or Memory Games grab this quickly, even if you're not ;-).

 QB-News  • Daily news  • Archived news  • QB Chat  • Qbasic Articles Learning center  • QB Books  • Qbasic Lessons!  • Qbasic FAQ  • Newbies Section  • Qbasic Tutorials Links  • RPG-Dev.net  • RPG Top 50  • V Planet  • QB Cult Magazine  • QB45  • GB Games  • TRINTS Online  • The QB Code Post  • Master Minds  • SIA  • Piptol Productions  • QB-Rpg Scene  • Qbasic Zone  • Jocke the Beast  • Delta Code  • Stars Dev  • Kentauri  • UltimateQB  • Fomalhaut Software  • Q-Tech

 Qbasic Classes
Welcome! In this chapter, we will cover the following commands:

DIM
FOR...NEXT
STEP
GOTO
IF...THEN
COLOR

As of right now, you know about variable types, PRINT, INPUT, and LET. These are the most basic parts of the BASIC environment. We now have to move into a more complex area.

Numbers are used in every application, no matter what kind of program it is. That's why variables are so important in BASIC. However, when dealing with large amounts of numbers and strings, hundreds of variable names would get too complex and unreadable. This is where ARRAYS come in.

Think of an array as a big box in the computer's memory seperated by dividers into smaller boxes. You can put different things in the small boxes, but organize them into one big box. This is exactly what an array does. An array is an area in the computer's memory seperated into smaller parts for different variables.

To create an array, you use the DIM command:

DIM ArrayName\$ (number)

ArrayName is the name of the array, followed by a variable type (\$, !, etc.) The number in parentheses is the amount of smaller "boxes" in the array.

To use an array, you assign values to the array using the LET command just as you would a regular variable. However, this time you have to spec- ify which part of the array you are assigning the value to. For example,

DIM Day\$(7)
LET Day\$(1) = "Sunday"
LET Day\$(2) = "Monday"

and so on. To PRINT one of these variables in the array, you would do so just like a regular variable:

PRINT "Today is "; Day\$(1); "."

And, accordingly, to get a value from the keyboard into the array, you use the INPUT command:

INPUT "What is Player 1's name"; players\$(1)
INPUT "What is Player 2's name"; players\$(2)

You can use an array element just as you would use a variable in any statement. Using arrays, you can manage hundreds of names, values, strings, dollar amounts, and much more.

So now you can use arrays. But wouldn't it be tedious, say, if you wanted to print the 100 different values of an array named players\$? It obviously would be wasteful to have 100 different print commands. This is where LOOPS come in.

A loop is a set of instructions that are repeated either a certain number of times, until a condition is met, or infinitely. The first loop command (or set or commands, really) we will study is the FOR...NEXT loop. It repeats the commands between the FOR and NEXT commands a certain number of times. Its syntax is simple:

FOR i = 1 TO 100
PRINT i
NEXT i

The program would print the numbers from one to a hundred. Why did we name the variable "i"? It's just a standard - one that has been in place for several decades. You can call it anything you want - "bob," "sam," "linda," or "z." You just have to adjust the code above. You can also change the lower and upper values of i - for example,

FOR i = 6 TO 371
PRINT "The magic number could be:"; i
NEXT i

Another way to change how the FOR...NEXT loop increments is by using the STEP command. If you omit the STEP command (as in a regular FOR...NEXT loop), "i" increases by one every time the commands in the loop are comp- leted. With the use of the STEP command, you can change this to any number you desire. For example:

FOR i = 100 TO 300 STEP 2
PRINT "The magic number might be:"; i
NEXT i

This would print all the even numbers from 100 to 300. You could also make "i" go backwards, as in this example:

FOR i = 300 TO 100 STEP -2
PRINT "The nifty numeral is now:"; i
NEXT i

FOR...NEXT loops can be used for just about anything. In the next chapter, I will talk about two other kinds of loops that keep going until a certain condition is met. But until then, (it won't be six months this time!) fool around with this loop.

The last command I'll talk about in this installment is the GOTO command. If you don't know about it, GOTO is a very simple yet extremely powerful command that lets you split up your program into smaller parts. Here's how you use it. You have to "label" lines that you want to go to using the GOTO command by assigning them numbers or names. For example:

topofprogram: CLS
PRINT "This is an infinite loop"
GOTO topofprogram

Or, this:

1 CLS
PRINT "Hello world!"
GOTO 1

And that's GOTO. Very simple, yet powerful. So how can it be use- ful? It's not very important to be able to just GOTO in a program without some reason to. GOTO becomes very useful when used in conjunction with the IF...THEN statement, which allows logical progression of your program based on a condition.

IF...THEN does exactly what its commands say. IF this is true, THEN do this. You can use it to create some type of menu, like this:

PRINT "Press 1 to clear the screen, or 2 to say 'Hello'!"
INPUT "What do you want to do"; choice
IF choice = 1 THEN GOTO clrscr
IF choice = 2 THEN GOTO hello
clrscr: CLS END
hello: PRINT "Hello, hello, hello!"
END

You can change the GOTO after the THEN.. statement to any valid QBasic command, like LET, PRINT, or INPUT. Or, you can replace the "=" with any mathematical symbol (like greater than(>), less than(<), or not equal(<>)) Here's a couple of examples in one program:

PRINT "Program Example #1"
PRINT "Try to guess the number I am thinking of between 1 and 10."
PRINT "You get 3 chances."
INPUT "First chance"; number
IF number = 3 THEN GOTO gotit
PRINT "Sorry!"
INPUT "Second chance"; number
IF number = 3 THEN GOTO gotit
PRINT "Ooh!"
INPUT "Last chance"; number
IF number = 3 THEN GOTO gotit
PRINT "Sorry! The number was 3!"
END
gotit: PRINT "You win! Good job!"

And that's your basic IF...THEN statement. The last thing we'll deal with in this chapter is a cosmetic command that will let you change the color of the text on the screen. It is the COLOR command, and lets you change the color of the text to one of 16 colors (including black, the background color). You use it in this form:

COLOR 13 PRINT "Magenta!" COLOR 7 PRINT "Grey!"

The number after the COLOR statement is one of these color codes:

00 - black
01 - dark blue
02 - dark green
03 - dark cyan
04 - dark red
05 - dark purple
06 - orange brown
07 - grey
08 - dark grey
09 - light blue
10 - light green
11 - light cyan
12 - light red
13 - magenta
14 - yellow
15 - bright white

That ends our discussion of the COLOR statement and our chapter. This chapter introduced a lot of new material that will allow you to accomplish a lot more in your QBasic programs. Good luck, and keep bugging Mallard to write a fourth installment in this series!

Exercises:

1. Write a guessing-game program that gives you three chances to guess a pre-determined number between 1 and 10. After each guess, if the guess is incorrect, have the program tell the user whether (s)he is too high or too low. If the user guesses correctly, give them a colorful "winner" message.

2. Create an array that stores all the months of the year, then create an array with all the days of the year. Write a program that displays all the months and days in a colorful form using a FOR...NEXT loop.

3. Fool around with all the commands in this chapter.

4. Have fun with programming and keep checking Qbasic News for programs, tutorials, and QBasic fun!

[ About | Lesson 4 ]