Programming Tutorial
Programming Tutorial
After launching the QBasic interpreter you might see a window requesting a list of
"parameters." If this window comes up, press the Enter key to continue.
You should now see the QBasic interpreter, which has a blue background and
displays a dialog box at the center. (If the interpreter fills the entire screen, then you
may want to press "Alt + Enter," to make it smaller.)
Type the following (including the quotation marks) in the QBasic interpreter:
Now press F5 to run the program. You should now see a black screen,
with Hello World at the top, and Press any key to continue at the bottom.
Chapter 2: Strings
There are certain types of data (or information) called "strings." Strings contain a
sequence of characters (letters, numbers, and symbols) enclosed in quotation
marks. For example, "Hello World!" is a string.
"0123456789"
"This is a string"
"abc123"
"1 + 1 = 2"
"!@#$%^&*()"
Commands
There are also special functions called "commands" (also called "instructions"). A
"command" tells the QBasic interpreter to do something.
The PRINT command tells the QBasic interpreter to print something to the screen. In
this case, the interpreter printed "Hello World!".
?"Hello World!"
With the PRINT command, you can also print numbers to the screen. Delete the
current program (unless you already have) and write the following:
PRINT 512 (or ?512)
<press Enter>
Press F5 to run the program. The program outputs:
512
Chapter 3: Expressions
An expression is something the interpreter calculates (or evaluates). Such as:
1 + 1 (returns 2)
3 * 34 (returns 102)
80 / 4 (returns 20)
^ 2 ^ 3 = 23
If you pass an expression to the PRINT command, the value returned (a number) is
printed.
Program output:
990
If you enclose the expression with quotation marks, the expression becomes a string
and isn't evaluated. For example:
PRINT "512 + 478"
Output:
512 + 478
CLS
To place World onto the previous line, place a semi-colon after PRINT "Hello".
PRINT "Hello";
PRINT "World"
Output:
HelloWorld
Also, if you put a comma instead of a semi-colon on the first line, the program will
insert spaces between the two words.
PRINT "Hello",
PRINT "World"
Output:
Hello World
Chapter 4: Variables
A variable is a piece of data kept in the computer's memory (RAM). The location of a
variable in RAM is called the "address."
This next program sets X to 15, and then prints the variable:
X = 15 x 15
print X
In the above example, the number 15 was stored in the computer's RAM at a certain
memory address. Then the PRINT command accessed (or looked at) that address
when it printed "15" to the screen.
num
VALUE
xYz
abc123
PRINT X
PRINT Y
PRINT Z
Output:
82
101
79
(NOTE: The memory addresses of these variables are not necessarily as specified)
Expressions
If you pass an expression to a variable, the expression is evaluated and the variable
is set to that value.
x = 500 + (10 * 7)
PRINT x
Output:
570
PRINT distance
Output:
100
PRINT Y
Output:
700
X = X + 1
Strings
If you add a dollar sign ($) to the end of a variable, the variable is a string.
X$ = "Hello World!"
PRINT X$
Output:
Hello World!
PRINT X$
Output:
HelloWorld
d$ = a$ + b$ + c$
PRINT d$
Output:
String1String2String3
One way to receive input from the keyboard is with the INPUT command.
The INPUT command allows the user to enter either a string or a number, which is
then stored in a variable.
INPUT data$
PRINT data$
When this program is executed, the INPUT command displays a question mark,
followed by a blinking cursor. And when you enter text, the program stores that text
into the variable data$, which is printed to the screen.
PRINT number
If you enter text instead of a number, the QBasic interpreter displays an error
message ("Redo from start").
The IF and THEN commands are used to compare an expression and then perform
some task based on that expression.
x = 5
Expression signs
You can also enter the following statements, instead of the equals sign:
x < 5 (x is less than 5)
x = 5
ELSE
Using the ELSE command, you can have the program perform a different action if the
statement is false.
x = 3
IF x = 5 THEN
PRINT "Yes"
ELSE PRINT "No"
Since X doesn't equal 5, the output is:
No
END IF
END IF allows you to have multiple commands after the IF...THEN statement, but
they must start on the line after the IF statement. END IFshould appear right after
the list of commands.
x = 5
IF (x = 5) THEN
INPUT a$
PRINT a$
END IF
IF (x = 5) THEN
INPUT a$
PRINT a$
ELSE
PRINT x * 2
END IF
Output:
32
ELSEIF
The ELSEIF command allows you to perform a secondary action if the first
expression was false. Unlike ELSE, this task is only performed if a specified
statement is true.
x = 6
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
END IF
Output:
Statement 2 is true
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
ELSEIF (x = 7) THEN
PRINT "Statement 3 is true"
ELSE
PRINT "No above statements are true"
END IF
Output:
No above statements are true
Multiple expressions
Made by: Sir Adnan Kazmi VLE Lecture
Roots IVY International School
QBASIC TUTORIAL
IG4/O4 Computer Science
You can have more than one expression in IF...THEN by using either
the OR operator or the AND operator.
The OR operator only requires one expression to be true in order to print "Yes" in the
following program:
x = 20
Output:
Yes
Strings in IF...THEN
So far in this chapter, we've only been dealing with numbers, but you can also use
strings with the IF...THEN command.
x$ = "Hello"
The GOTO and GOSUB commands enables you to jump to certain positions in your
program. Labels are used to specify what point in the program to continue
execution.
GOTO
To use GOTO, place a label somewhere in your program, and then enter.
GOTO <label>
GOTO TheLabel
PRINT "2"
TheLabel:
PRINT "3"
Output (notice how PRINT "2" is skipped):
1
3
GOSUB
The GOSUB command is the same as GOTO, except when it encounters
a RETURN statement, the program "returns" back to the GOSUB command. In other
words, RETURN continues program execution immediately after the
previous GOSUB statement.
PRINT "1"
GOSUB TheLabel
PRINT "2"
END
TheLabel:
PRINT "3"
RETURN
(Note: The END command exits the program.)
Since the program returns to the GOSUB command, the number 2 is printed this time.
1
3
2
Line numbers
"Line numbers" can be used as labels.
PRINT "1"
GOTO 10
PRINT "2"
20 GOTO 40
30 PRINT "2"
40 PRINT "3"
2 GOTO 160
Guessing game
The following is a simple guessing game:
CLS
start:
PRINT "Guess a number between 1 and 10: ";
INPUT num
IF (num = 6) THEN
PRINT "Correct!!!"
ELSE
PRINT "Try again"
PRINT
GOTO start
END IF
Output (may be slightly different):
Guess a number between 1 and 10: ? 2
Try again
Chapter 8: Loops
"Loops" make it easier to do an action multiple times. There are at least four types of
loops: IF...GOTO, WHILE...WEND, DO...LOOP, andFOR...NEXT.
IF...GOTO
This program uses IF...GOTO to create a loop:
x = 10
start:
PRINT x
x = x + 1 (This adds 1 to x)
WHILE...WEND
The WHILE...WEND commands continue a loop until a specified expression is false.
To use WHILE...WEND:
1. Place an expression after WHILE
WHILE x < 15
PRINT x
x = x + 1
WEND
Output (same as in previous example):
10
11
12
13
14
DO...LOOP
DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight
advantages. With DO...LOOP you can:
1. Loop until an expression is true
2. Loop at least one time regardless of whether the expression is true or not.
To use DO...LOOP:
1. Specify whether the loop continues "while" the expression is true
or "until" the expression is true, using the WHILE and UNTIL statements,
respectively.
DO WHILE x < 15
PRINT x
x = x + 1
LOOP
DO UNTIL x = 15
PRINT x
x = x + 1
LOOP
If you place the expression at the end of the loop instead, the program goes through
the loop at least once.
x = 32
DO
PRINT x
x = x + 1
FOR...NEXT
FOR...NEXT provides an easier way to create a loop.
FOR x = 1 TO 5
PRINT x
NEXT x
Output:
1
2
3
Also, you can use the STEP attribute to specify how much X will be increased each
time through the loop.
FOR x = 1 TO 5 STEP 2
PRINT x
NEXT x
Output:
1
3
5
STOPPING LOOPS
To stop a loop prematurely, use the EXIT command, followed by either FOR or DO.
FOR x = 1 TO 5
PRINT x
NEXT x
Output:
1
2
3
(NOTE: This command only works with
the DO...LOOP and FOR...NEXT commands, not
with WHILE...WEND or IF...GOTO.)
Documenting your program (also called "commenting") allows you to remind yourself
about something in your program. Plus, if your program is seen by other people,
documenting can help them understand your code.
The REM (remark) command enables you to add comments to your program without
the text being treated like an instruction.
CLS
PRINT "Some text"
You can add REM to the same line as another command by placing a colon after the
first instruction.
CLS: REM This command clears the screen
PRINT "Text": REM This command prints "Text" to the screen
PRINT 534: REM This prints the number 534 to the screen
NOTE: If you use an apostrophe instead of REM while doing this, you do not need to
add a colon.
CLS ' This command clears the screen
PRINT "Text" ' This command prints "Text" to the screen
PRINT 534 ' This prints the number 534 to the screen
Mathematics functions
SQR
Use SQR to find the "square root" of a number.
PRINT SQR(1)
PRINT SQR(4)
PRINT SQR(9)
PRINT SQR(16)
PRINT SQR(25)
Output:
1
2
3
4
5
ABS
ABS returns the absolute value of a number. In other words, ABS converts a
negative number to a positive number (if you pass a positive number,ABS does
nothing).
PRINT ABS(12)
PRINT ABS(-12)
Output:
Made by: Sir Adnan Kazmi VLE Lecture
Roots IVY International School
QBASIC TUTORIAL
IG4/O4 Computer Science
12
12
PRINT COS(PI / 4)
PRINT SIN(PI / 3)
PRINT TAN(-PI / 2)
PRINT ATN(TAN(-PI / 2))
Output:
.7071067
.8660254
6137956
1.570796 (Same as PI / 2)
An array is a list of variables of the same type. Arrays are useful for organizing
multiple variables. To create an array, use the DIM (dimension) command.
PRINT a, b, c, d, e
Output:
2 4 6 8 10
FOR x = 1 to 5
vars(x) = x * 2
NEXT
FOR x = 1 to 5
PRINT vars(x),
NEXT
Output:
2 4 6 8 10
Strings
You can also create an array of string variables.
DIM vars$(5)
vars$(1) = "Two"
vars$(2) = "Four"
vars$(3) = "Six"
vars$(4) = "Eight"
vars$(5) = "Ten"
The non-string variables we've used in this tutorial are actually called single-
precision variables. These types of variables (SINGLE's) are used to store numbers
that can contain a decimal value (such as 1.89 or 3.141593). Since they have
decimal values, they are also known as "floating-point" variables.
LONG - Same as INTEGER, but can contain numbers between -2,147,483,648 and
2,147,483,647.
var1 = 15.28
var2 = -2000000000
var3 = 12345678.12345678
PRINT var1
Made by: Sir Adnan Kazmi VLE Lecture
Roots IVY International School
QBASIC TUTORIAL
IG4/O4 Computer Science
PRINT var2
PRINT var3
Output:
15 (Notice how the decimal value is removed)
-2000000000
12345678.12345678
To do so, place one of the following at the end of a variable (or number):
! (single--actually, this doesn't change anything)
% (integer)
& (long)
# (double)
PRINT var1%
PRINT var2&
PRINT var3#
Output:
15
-2000000000
12345678.12345678
To create a subroutine:
1. Go to the "Edit" menu
2. Select "Untitled"
SUB MySub
CALL GetText
CALL GetText
CALL GetText
CALL GetText
CALL GetText
CALL GetText
CALL GetText
SUB GetText
END SUB
SUB GetText
PRINT "Enter some text:";
INPUT text$
PRINT "The text you entered was: "; text$
END SUB
Parameters
Parameters are numbers and strings that you pass to a subroutine, much like a
QBasic command.
' This passes 16 as a parameter:
CALL OutputNumber(16)
PRINT num
END SUB
Output:
16
COMMON SHARED x$
Functions
To return a value, set a variable with the same name as the function.
PRINT Add(10, 7)
END FUNCTION
Output: 17
Since a function can return a value, the name of the function can end with special
characters (see Variable types, Using special characters).
END FUNCTION
Output:
HelloWorld
Hexadecimal numbers
We generally use the base 10 (decimal) numbering system, where each digit must
be between 0-9; but the "hexadecimal" system (base 16) can also have
digits A, B, C, D, E, and F (16 total digits).
0 = Zero
1 = One
2 = Two
3 = Three
4 = Four
5 = Five
6 = Six
7 = Seven
8 = Eight
9 = Nine
A = Ten
B = Eleven
C = Twelve
D = Thirteen
&H110
Binary numbers
The "binary" system (base 2) can only have two digits, 0 and 1. Therefore, no binary
number has a digit between 2 and 9.
0 = Zero
1 = One
10 = Two
11 = Three
100 = Four
101 = Five
110 = Six
111 = Seven
1000 = Eight
1001 = Nine
1010 = Ten
1011 = Eleven
1100 = Twelve
1101 = Thirteen
1110 = Fourteen
1111 = Fifteen
10000 = Sixteen
10001 = Seventeen
10010 = Eighteen
10011 = Nineteen
10100 = Twenty
.
.
.
Notice how binary numbers can be found by excluding numbers that have a 2, 3, 4,
5, 6, 7, 8, or 9.
0
1
2
3
Made by: Sir Adnan Kazmi VLE Lecture
Roots IVY International School
QBASIC TUTORIAL
IG4/O4 Computer Science
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.
.
.
97
98
99
100
101
102
103
104
105
106
107
108
109
110 TIP: Binary (and hexadecimal) numbers are often
111 written with leading 0's.
112
113 0000 (same as 0)
114 0001 (same as 1)
115 0010 (same as 10)
. 0011 (same as 11)
.
.
In base 10, as explained above, we multiply ten for each time a digit goes to the left.
10 = 10
100 = 10 * 10
1000 = 10 * 10 * 10
10000 = 10 * 10 * 10 * 10
.
.
.
But in binary, we multiply by two.
10 = 2 (2)
100 = 2 * 2 (4)
Bits
A "bit" is the smallest piece of data stored in your computer's memory. The value of a
bit can be either 0 or 1. All data in your computer has a certain number of bits.
Bytes
A "byte" is 8 bits, and can have a value between 0 and 255 (or, in binary,
between 0 and 11111111). A character, such as Q, takes up one byte of memory.
This is because there are 256 different characters.
(If you don't fully understand bits and bytes, don't worry about it.)
Each memory address is divided into two parts: segments and offsets. See the
figure below.
A memory address such as 12345678 (in hexadecimal) has a segment of 1234 and
an offset of 5678.
A segment can have a value between 0 and 65535 (or between 0 and FFFF). An
offset can be within the same range.
You can find out a memory address of a piece of data by multiplying its segment
by 65536 (or 10000, in hexadecimal) and then adding its offset to the result. In
QBasic, you can get a variable's segment by using VARSEG and its offset by
using VARPTR.
segment = VARSEG(x)
offset = VARPTR(x)