Qbasic Tutorial : Learn Qbasic in a fast, simple,short, and intuitive way. Honest Effort Required.

--- Qbasic ---



Exercise : Write a program to sum 1 + 1.5 + 2 + 2.5 + 3 + 3.5 + .... + n , where n is any positive integer.

[Download Program]
[Download Program]

There are two ways to accomplish this. The first one is our usual way, the second one uses a Qbasic feature "For ... step ... next .. ". Please study carefully the construct of the for-loop.

Exercise : Write a program to print 1, 2, 3, ...., 1000 on the screen

Ans :

for x = 1 to 1000
print x;
next x

Exercise : Find out the difference of using the 3 forms of print statement in the above program,

print x
print x,
print x;

Exercise : Write a program that prints the square root of 2, 3, ... up to 50. (Note : sqr(x) is a Qbasic built-in function).

         2     1.4142
         3     1.7321
         4     2.0000
Use for-loop and print the result to 4 decimal places.

Ans : (if step=1 , then we may omit it).

for x = 1 to 50
print using " ## ##.####"; x, sqr(x)
next x
[Download Program]

Exercise : Write a program to print sin( ), cos( ), tan( ) from 0o to 90o. Print the result with 4 decimal places. Also note that tan(90) is undefined, so do not print tan(90).

Output should look somewhat like that below
         Angle        sin          cos       tan

           0         0.0000      1.0000     0.0000
           1         0.0175      0.9998     0.1746
           2         0.0349      0.9994     0.0349

Ans :

[Download Program]

Notice that we use a variable "linecount", so that the program will pause after every 23 lines have been printed.


[Download Program]
[Download Program]
[Download Program]

Note : cls is a DOS command, it is to clear the screen. In Qbasic, there is also a "cls" statement, and its function is the same.

Exercise : plot square root from 0 to 10. First plot with points only. Next plot with line. (Note: find out the Qbasic key word "line (x0,y0)-(x1,y1)" from on-line manual.)

Exercise : plot tan(x), from -89 to 89, first plot with points, next plot with line.

Exercise : look up QBASIC online manual (either shift+F1, OR move cursor under keyword, then press F1), repeat the above 2 exercises, this time, plot with yellow color.

Some more String functions

Exercise : We discussed string in the last chapter. There are many Qbasic built-in functions about string, e.g. left$(...), right$(...), mid$(....). (left part of string, right part of string, middle part of string). Guess what will be the result of the following program. (Note: please read on-line manual.)

a$ = "This is a test"
b$ = "Hong Kong and Kowloon"
c$ = a$ + b$
d$ = left$(a$,4)
e$ = right$(b$,7)
print c$
print d$
print e$

Exercise : find out from on-line manual, what the following Qbasic functions do:

Ans : Do not mind if you don't understand them all. You will after you gain more experience.
lcase$(a$) change that string to lower case.
ucase$(a$) change it all to upper case.
chr$(30) returns a string consisting of only 1 character (= 1 byte), 30 is its ascii value.
asc("a") returns the ASCII value of "a", which should be 97.
str$(12345) turns a numeric value into a string.
val("12345") turns a string consisting only of digits and decimal point into a number.
instr(a$,b$) searches for b$ within a$, and returns its position. If b$ is not found in a$, it returns 0.