Ever want to learn QBasic? Ever had the desire to learn how to program your very own programs and games! Well, you can start here. The QBasic tutorial is meant to teach the basics on QBasic. Then as you get advanced, you will start learning on your own.
Lesson 1 (Print, Cls, Variables)
Printing stuff on the screen, clearing the screen, and how to implement
Variables in your coding.
1. In the dos prompt, type Qbasic and hit enter.
Type this in the screen that says "Untitled"
10 CLS
20 Print "Planet Basic is COOL!"
Now run your program by hitting F5. The Cls stands for 'Clear Screen' which clears the screen! :) The Print command prints the text in between the "" on the screen. The line numbers are there for keeping track and for LOOPS (You will learn more about them in a later lesson). Now let's try a little longer program.
10 CLS
20 a$ = "Whoa. How'd it do that"
30 Print a$
40 Print ":-)"
Now, this program is a little harder. A$ is a variable which we used to store "whoa. How'd it do that" in it. Why the $ sign? We put the $ sign there because we saved text to that variable. If we we're storing numbers, we would use an 'a' without the $ to represent the variable. You can use any letter in the alphabet for variables. Here is another program which uses a variable to store numbers.
10 CLS
20 a = 2000
30 b$ = "The new millenium is the year"
40 Print b$; a
Pretty simple! Well, now you know what CLS means, what Print does and what a Variable is and does! You passed lesson 1!
Lesson 2 (Input, beep, color)
How to ask the user questions, making a sound, and changing colour.
1. In the dos prompt type Qbasic and hit enter.
Type this in the screen that says "Untitled"
10 CLS
20 Input "What is your name"; a$
30 Beep
40 Print "Hello" + " " + a$ + "!"
50 Print "You are the master"
In this example we asked the user to input his/her name. The program printed, "Hello (your name)" We also placed BEEP in there. What beep does is make a sound using your 'Pc Speaker'.
Now let's try some Colour usage.
10 cls
20 color 9
30 print "BLUE RULES!"
40 color 10
50 Beep
60 print "But dont forget GREEN!"
'Blue rules' should be in blue, and 'But don't forget GREEN!' should be in green. PRETTY KOOL HUH? :-)
Now let's try combining the two.
10 cls
20 color 10
30 input "How are you today" a$
40 print "Well i'm glad your" + a$
50 Beep
Isn't it cool....
Lesson 3 (For next Loops)
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. The computer repeats the commands between the FOR and NEXT commands a certain
number of times. The syntax is simple:
FOR i = 1 TO 100
PRINT i
NEXT i
The program will 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 completed. 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
Lesson 4 (Randomize Timer, while wend loops)
x = INT(RND * 10)
+ 1
The above statement would give you a number between 1 and 10. To get a number
between 0 and 10, just get rid of the "+ 1" portion of the line. That's all you
have to do for random numbers - just change the RND number to get your upper boundry.
WHILE <variable >
= < number>
...
WEND
That's it. Let's say you want to make a simple guessing game. All it takes is a simple
loop, as in the next example.
RANDOMIZE TIMER
PRINT "Guessing game!"
number = INT(RND * 10) + 1 'random number from 1 to 10
PRINT "The number is from 1 to 10"
WHILE guess <> 10 'loop until the guess = 10
INPUT "Guess"; guess
tries = tries + 1 'count the number of tries
WEND
PRINT "Good job! You got it in"; tries; " tries!"
END
There's a working, and neat program in only 10 lines! That's all there is to a
WHILE...WEND loop. So simple, and yet so useful!
I hope our tutorial has helped you understand qbasic a little more and best wishes to all of you future programmers :-)