QBasic Mini-Tutorial
Speakers know the advice of "Tell them what you're going to tell them ...",
which is what this very short mini-tutorial does. It's a preview of the
full tutorial provided at this site,
with all the fat (and some of the meat) cut out.
There's no discussion - just notes and comments.
It's an introduction for newbies to get a sense of QBasic in one reading,
although some sections may not make much sense until you've read the corresponding
tutorial. And for those of you coming back to QBasic after an absence,
it's a reminder of what QBasic offers and perhaps what you forgot.
A Simple Program
Just to get you started, here's a quick look at a traditional "Hello World" program.
msg$="Hello World" # assigns a string value to the variable msg$
print msg$ # prints value of the variable msg$ to the screen
Executing a QBasic Program
You can run a QBasic program in two ways.
Syntax
statements end at end of line print "hi"
multiple statements per line print "hi" : print "bye"
comments REM line starts with REM
comments 'right of apostrophe
double quotes only (no single quotes) "yes" but not 'no'
procedure parentheses optional print("Hello") : print "Hello";
Operators
Arithmetic standard + - * / =
integer division \
exponentiation ^
Numeric Compare = != < > <= >= <=>
Boolean and or not xor eqv imp
false = 0
true = non-zero
Declarations
dim, redim, const # declaration
defint, defdbl, defsng, defstr, deflng # declared by letter
static, shared # scope
type # user-defined type
data, read, restore # numeric resources
Variables
declaration not required
declaration Dim x$, y as Long
variable type suffixes single x! integer x%
double x# long x&
string x$
arrays zero-based by default Dim x$(5), y$(3 to 5)
x$(1) = "test"
Variable Scope
variable scope - limited to where declared
- scope is global with SHARED
procedure variables - local to procedure
- STATIC stores values between calls
Subroutines
Declaration required - Declare MySub (a$, b%)
Declare MyFunction$ (a$, b%)
Argument list: a as long, b as integer, c as ANY
Function MyFunction$ (arg1, arg2, ... argn)
...
End Function
Sub MySub (arg1, arg2, ... argn)
...
End Sub
passed variables - procedure changes have global affect
- enclose in () to prevent change
calling procedures - call MySub(a$,b$) 'with call, use ()
MySub a$, b% 'without call, no ()
Flow Control
if (expr) then ... elseif ... elseif ... else ... end if
select case (expr) ... case ... case is ... end case
on (expr) gosub
on (expr) goto
for var=start to end ... next var
Do unless|until (expr) ... Loop
Do ... Loop unless|until expr
while (expr) ... wend
gosub
goto
stop, end, exit, return, system # Program/Loop redirection
Functions (commonly used, built-in functions)
Conversion cint, cdbl, csng, clng # number to type
cvd, cvi, cvs, cvl, cvsmbf, cvdmbf # string to type
Arithmetic let, swap, clear # assignment
data, read restore # program data
exp, log, sqr # logarithms
hex$, oct$ # conversions
abs, sgn, fix, mod, int # conversions
rnd, randomize # random
Strings ucase$, lcase$ # capitalize
val, len # string variable properties
asc, chr$ # single character functions
left$, right$ # truncate
ltrim$, rtrim$ # remove end spaces
mid$, instr # find/replace
rset, lset # justification
space$, string$ # return lengths of strings
cvi, cvl, cvs, cvd # convert numbers to strings
str$, mkd$, mki$, mks$ # convert numbers to strings
mkdmbf$, mkl$, mksmbf$ # convert numbers to strings
let, swap, clear # assignment
Trig atn, cos, sin, tan # standard trigonometry functions
Arrays ubound, lbound # properties
erase # reset values
option base # set minimum index
Time date$, time$ # properties
timer, sleep, wait # intervals
Printing lprint, lprint using # print commands
lpos, spc, tab, width # modifiers
Files/Directories
bload, bsave, def seg # binary actions
lock, unlock # access control
open, close # file access directive
freefile, clear, eof, reset # file status
kill, name # file management
write, print, print using # write to file
input, input$, line input # read from file
get, field, put, seek # random file actions
files, loc, lof, fileattr # file information
chdir, mkdir, rmdir # directories
open "myfile.txt" for input as #1
close #1
read 1 line of file using scalar line input#1, var$
read all lines into array while not eof(1)
var$ = line input #1
wend
write line into file print #1, var$
write line into file (formatted) write#1, var1$, var2
chdir, mkdir, rmdir # directories
Keyboard
input, input$, line input # keyboard input
inkey$, key, key(n), On Key # individual keystrokes
Sound
play, beep, sound # create sounds
on play, play # event trapping
Screen (Text)
width, view print, screen, color # properties
csrlin, pos, locate # cursor status
print, print using, write # write to screen
tab, spc # print modifiers
Screen (Graphics)
screen, window, view, pmap # mode/coordinates
color, palette, palette using # color control
cls, pcopy # page control
circle, line # define shapes
draw, paint # custom shapes/fill
pset, preset, point # points
get, put # screen image capture/display
Event Trapping
on strig, strig, stick # joystick
on key, key # keyboard
on pen, pen # light pen
on com, com # serial ports
on play, play, beep, sound # speaker
on timer, timer # timers
ioctl, ioctl$ # device control strings
Errors
tron, troff # trace execution
erdev, erdev$ # device identification
erl, err # error information
error # error simulation
on error, resume # error trapping
System Calls
environ, environ$ # access environmental variables
fre # available string space
inp, out # port access
peek, poke # memory access
shell, chain, common, run # transfer control/pass variables
varptr, varptr$, varseg # pointers
call absolute # call assembly language program
If you have any suggestions for additions to this mini-tutorial, please let me know.
|