QBASIC Tutorial
QBASIC Tutorial
QBASIC Tutorial
If you haven't already done so download the QB64 compiler from here
: http://www.qb64.net/. Select your operating system and install.
Try typing this into the qbasic compiler (It's a blue window) :
Now, press 'F5' to run the program (Or go to the 'run' menu and click start)
What happened? It probably displayed the message 'Hello World' on the screen right?
Commands
PRINT
The first command you're going to learn about is the print command. Basically
what 'Print' does is tell Qbasic to write something to the screen. In the program above,
Qbasic printed the message "Hello World".
*HINT*
Instead of typing print into the blue window/compiler, just type ? "Hello World".
When you press enter/return, the '?' will be changed to PRINT
*HINT*
Try this :
x=1
Print x
Try this :
x=1
? "Hello World" ; x
MATHEMATICAL OPERATORS
REM
Rem doesn't actually do anything. It stands for remark and is used to add comments to
the code. The compiler COMPLETELY IGNORES anything typed after REM.
Try this
For example :
Now, just by looking at the REM statement, other programmers would know what the
program does, without having to read the code.
For example :
INPUT
What use is a computer if you can't tell it what to do?
With Input, you can ask the user to enter a value. Just like print, you can type many
things separated by semicolons (;).
Try this :
Input "Enter your name" ; name$ 'Whatever the user types is put in the variable
name$
Print name$
IF...THEN...ELSE
If you've taken any science classes you've probably been taught to write your
hypotheses in "IF...THEN...BECAUSE" for. In QBASIC, the format is this :
IF (condition is met) THEN (do this task) ELSE (otherwise do this. NOTE : THE
"ELSE" PART IS OPTIONAL, THE "THEN" PART IS NOT!!)
These are the statements usually used in the (condition is met) part
> (greater than) : If the first value is more than the second, the condition IS met
otherwise it is NOT
< (less than) same as above except if the first value is LESS than the second, the
condition IS met otherwise it is NOT
>=(greater than OR equal to) if the first value is greater than or equal to the
second, the condition IS met otherwise it is NOT
<= (less than OR equal to) if the first value is less than the second, the condition
IS met otherwise it is NOT
= (equal to) (I think this one is pretty obvious)
<>(NOT equal to) (again, its quite obvious)
More conditionals
In an IF...THEN statement you can have multiple conditions."How" you say? I'll show
you...
OR
The OR statement tells QBASIC to do the action after "THEN" if either of the
conditions are met.
For example,
If num>10 OR num <5 then print "That's a good number" 'If num is more than 10 OR
num is less than 5 then it will print the message
AND
The AND statement tells QBASIC to do the action after "THEN" if ALL of the
conditions are met.
For example,
If num>10 AND num2 <5 then print "That's a good number" 'If num is more than 10
AND num2 is less than 5 then it will print the message.
NOT
The NOT statement tells QBASIC to do the action after "THEN" if a condition is NOT
met.
For example,
If NOT(num>5) then print "num is NOT greater than 5" ' If num is NOT greater than
5, then it will print the message
THERE ARE MORE CONDITIONAL, BUT FOR NOW, THESE ARE THE
IMPORTANT ONES. Some of the other conditionals include : XOR, NAND
(basically NOT + AND) , and NOR (basically NOT + OR).
Modifying a String
Modifying strings is very useful in a number of places. I'll show you some basic
commands which modify strings.
LEFT$
This command will take the left part of a string. For example, if i created a string called
str$ and stored "Hello,I am" in it and I wrote the following command :
modified$ = left$(str$,5)
modified$ would now be equal to the first 5 characters of the string "str$", which
means, modified$ would be "Hello".
RIGHT$
RIGHT$ is just like LEFT$ except it takes part of the string starting from the right.
modified$ = right$(str$,5)
*Just a thought*
What would you do if you wanted to take the middle part of the string?
In the previous example with the string "Hello,I am" what would I do if I only wanted
the "o,I" part of the string?
BUT WAIT! *drum roll please* THERE'S AN EVEN BETTER WAY TO DO THIS!
CHECK THE NEXT COMMAND!
*Just a thought*
With mid$ you can take the middle of a string by specifying where you want to start
from and how many letters you want.
With the previous example (I will use a new example for the next command!) if I
wanted the string "o,I" I would do this :
modified$ = mid$(str$,5,3) ' This tells QBASIC to start at character 5 (which is "o")
and take 3 total characters ("o,I"). MUCH EASIER THAN USING RIGHT$ AND
LEFT$!
UCASE$
Have you ever wanted to make plain text seem like shouting? (It's alright,neither have I)
For example :
LCASE$
Are you annoyed when people type in all caps?
For example,
*WAIT*
You might be thinking "Why would I ever need to use these commands?"
The answer to that is simple. Lets say that I wrote the follow program :
if yn$ = "y" then print "you said YES" else print "you said NO"
BUT, what if the user entered capital letter "Y" instead of "y"?
if LCASE$(yn$) = "y" then print "you said YES" else print "you said NO"
*WAIT*
LOOPS
In most programs, we need to be able to do the same thing over and over again. For
example, if I wanted to make a program which tells me the square root of the number
entered by a user, I would want it to keep going until I closed the program. Loops allow
us to do this. There are two main types of loops in most programming languages : The
FOR...NEXT loop and the DO...LOOP. In QBASIC there are also modifications of the
DO...LOOP, which we'll discuss later.