0% found this document useful (0 votes)
15 views8 pages

qb64 Notes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

QBasic Commands

When you open QBasic, you see a blue screen where you can type your program.

Let’s begin with the basic commands that are important in any program.

LET

The LET command in QBASIC is used to assign a value to a variable.

LET x = 10

LET y = 5

LET z = x + y

PRINT z

This assigns 10 to x, 5 to y, and then adds them together to give z = 15.

In QBASIC, LET is optional. You can also write x = 10 instead of LET x = 10.

PRINT

Command PRINT displays text or numbers on the screen.


The program line looks like this:

PRINT "My name is Nick."

Type the highlighted text into QBasic and press F5 to run the program. On the screen you’ll see:

My name is Nick.

You must put the text in quotes, like this – "text". The text in quotes is called a string. If you put
the PRINT alone, without any text, it will just put an empty line. PRINT can also put numbers on
the screen. PRINT 57 will show the number 57. This command is useful for displaying the result
of mathematical calculations. But for calculations, as well as for other things in the program, you
need to use variables.

Variables

When you think, you keep words or numbers in your mind. This allows you to speak and to
make calculations. QBasic also needs to keep words or numbers in its memory. To do this, you
use variables, pieces of QBasic memory, which can keep information. A variable can be named
with any letter, for example – a. It can also have a longer name, which can be almost any word.
It is important to know that there are two main types of variables – that keep a number and that
keep a word or a string of words.

Numeric variables. It’s basically variables named with just a letter or a word. You tell this
variable to keep a number like this:

a = 15

In other words, you assigned the value 15 to the variable a.


QBasic will now know that the variable named a keeps the number 15. Now, if you type:

PRINT a

and run the program, the computer will show this number on the screen.

String variables can keep so called "strings", which is basically any text or symbols (like % or £),
which you put in the quotes "". You can also put numbers in a string variable, but again, you
must include them in quotes, and QBasic will think that those numbers are just a part of text.
The string variables look like this – a$. The $ sign tells QBasic that this variable contains text.

Example:

a$ = "It is nice to see you" PRINT a$

On the screen you’ll see:

It is nice to see you

The PRINT command can print more that one string on the line. To do this, put the ; sign
between the variables. For example, you have two variables – name$, which contains name
Rob, and age, which contains the number 34. Then, to print both name and age, you type:

PRINT "Name - "; name$; ". Age - "; age

As you can see, the name of a variable can be more than just one letter – it can be a short word
which describes what sort of information does this variable keep.
What you see on the screen when you run the program will look like this:

Name – Rob. Age – 34

Or, you can type the program like that:

PRINT "Name - "; name$ PRINT "Age - "; age

The result is:

Name – Rob Age – 34

INPUT

INPUT is a command that allows you or anybody else who runs the program to enter the
information (text or number) when the program is already running. This command waits for the
user to enter the information and then assigns this information to a variable. Since there are two
types of variables, the INPUT command may look like this – INPUT a (for a number), or INPUT
a$ (for a string).
Example (Type this program into QBasic and run it by pressing F5)

PRINT "What is your name?"


INPUT name$
PRINT "Hi, "; name$; ", nice to see you!"
PRINT "How old are you?" INPUT age
PRINT "So you are "; age; " years old!"
END

The END command tells QBasic that the program ends here.

You don’t have to use PRINT to ask the user to enter the information. Instead, you can use

INPUT "Enter your name"; name$

and the result will be the same.

Mathematical Calculations

QBasic was obviously created for us to have fun, play games, draw nice graphics and even
make sounds. But, as you might guess, nothing good comes without a bit of effort that has to be
put in it. In the most QBasic programs a bit of math has to be done. The math… Doh! If you hate
mathematics, don’t worry. Qbasic will do it all for you, you just need to know how to tell QBasic
to do that.
Qbasic can perform the following mathematical operations:

Operator What it Example Result


does

+ Add 7+2 9

- Subtract 7-2 5

* Multiply 7*2 14

/ Divide 7/2 3.5

Example 1:

a = 15 / 4 + 3 PRINT a

Result on the screen:

Example 2:

PRINT "Enter the first number"

INPUT a

PRINT "Enter the second number"

INPUT b

c = a + b

d = a * b

PRINT a; "+"; b; "="; c


PRINT a; "*"; b; "="; d

END

When you run this program it goes like this:


Computer:

Enter the first number

You:

Enter the first number 22

Computer:

Enter the first number 22 Enter the second number

You:

Enter the first number 22 Enter the second number 18

Computer:

Enter the first number 22 Enter the second number 18 22 + 18 = 40 22 * 18 = 396

Arithmetic What it does Example Result


Operator

\ divides and turns the result 7\2 3


into

an integer(the whole
number)
^ Raises a number to the 3^4

power of another number (means: 3*3*3*3) 243

2.5 ^ 3

(means: 15.625
2.5*2.5*2.5)

MOD Divides two numbers, and if 17 MOD 5 2


the
(because: 17 / 5 =
result is not an integer 3.4

(for example - 3.25), finds 17 – 2 = 15


out
15 / 5 = 3)
how much to subtract from
the first

number in order to get the

integer result.

How QBasic decides what to do

In many programs (for example - games), the user has a choice of what to enter. In this case,
QBasic has to check what the user has typed, and to react accordingly. This can be done with
the IF...THEN command.

IF…THEN…ELSE

This command checks if an argument(condition) involving a variable is true. An argument may


look like this: IF a = 15 THEN... If the argument is true (and a really equals to 15), then QBasic
executes the command you put after the IF...THEN.
Example:

IF a = 15 THEN PRINT "OK"

If the argument is not true (if a is not equal to 15), QBasic bypasses this line and goes to the
next. In some cases, you can use the ELSE command, which tells QBasic exactly what to do if
the argument is not true.

IF a = 15 THEN PRINT "OK"


ELSE PRINT "It's not 15"

This example means that if a equals to 15, the computer will show OK on the screen:

OK

But if a is not equal to 15, you'll see:

It's not 15

To check the argument in IF…THEN command, you can use any of these mathematical
operators:

Relational Operator Meaning Example

= Equal to IF a = 15 THEN…
<> Not equal to IF a <> 15 THEN…

< Less than IF a < 15 THEN…

<= Less or equal to IF a <= 15 THEN…

> More than IF a > 15 THEN…

>= More or equal to IF a >= 15 THEN…

You might also like