--- Qbasic ---
This is the last chapter, and if you have done all the exercises from the first chapter up to now, even though that may be very time consuming, you should be able to write lot of programs. You will find that programming is an interesting thing to do, and useful as well.
You would probably agree that Qbasic is really easy to learn. One need not learn a lot to be able to program. There are lots of features in Qbasic that are seldom used, and they are there because of history.
I believe in the principle KISS ( = Keep It Simple Stupid ), so I concentrate on important matters so as not to waste your time.
In this last chapter, I will discuss some topics relating to programming.
" Programming for Bugs, dry-run flowchart, dry-run program, modular programming, alphabetical list of variables used " , all these are important precepts, I hope you would hold them fast.
Diagram speaks louder than words, hence flowchart is an very important communication tool. Moreover, Microsoft's "paint" program is very well-designed, and you will find it quite useful in drawing flowcharts. In fact, I use that to prepare many chapters in Mathematics too.
I hope you will form the habit of "Communication through graphic means."
Many people will tell you not to use "goto" statement. You MUST NOT listen to them, if you do, programming will become burdensome and wearisome. If you don't, programming will forever be a joy to you.
Let me tell you how this "no goto" trend come about.
Many years ago, the main programming languages were COBOL, FORTRAN. At that time, "if" statement was very simple, e.g. in Fortran, the if-statement is
if (....) goto ....Hence people must use lots of "goto". This, coupled with the fact that labels do not have to be consecutive, (FORTRAN) e.g.
1000 .....
if (....) goto 20
700 .......
.......
if (....) goto 300
......
5000 ......
.....
60 .....
...............
people really have a difficult time to trace programs.
Some computer scientists found that, if we introduce constructs like
But now one has to learn a lot of constructs, instead of learning one simple "goto".
Was the situation really that bad in the old days? Not so. Programmers have long discovered some "good programming habits". Consider the following piece of (FORTRAN) code :
if ( .not. condition1) goto 200
......
......
......
goto 1000
200 if ( .not. condition2) goto 400
......
......
......
goto 1000
400 if ( .not. condition3) goto 600
......
......
......
goto 1000
600 if ( .not. condition4) goto 800
......
......
......
goto 1000
800 if ( .not. condition5) goto 1000
......
......
1000 .........
Moreover, labels are strictly in ascending order. As you can see, the above coding is very clear in its intent, and, in my opinion, is in fact much better than the "no goto" but levels upon levels of nested "if" statements common nowadays.
On the keyboard there are function keys F1, F2, ...., F12. The following program demonstrates their use :
DEFLNG I-N
DEFDBL A-H, O-Z
ON KEY(1) GOSUB L40
KEY(1) ON
L20:
FOR i = 1 TO 4
SOUND i * 200 + 500, 5
NEXT i
GOTO L20
L40:
PRINT "You have just pressed F1"
RETURN
This program makes sound continuously, but when one presses F1, it will print "You have just pressed F1".
This is called "interrupt" processing. Computer will stop its current job, save the "status/registers" of the current program, branch to that subroutine, execute that subroutine. When finished, it reload the "status/registers", and continue from where it has stopped.
In Qbasic, there are other statements about "interrupt", e.g.
(Note : computer will execute that subroutine every N seconds)
(Note : it is used to sense joysticks, and is used mainly on games.)
(Note : it is intended for light-pen, but people usually use mouse nowadays.)
But a more sensible use of F1, .... , F12 is to "chain" programs,
(Syntax : chain "C:\test\prog1.bas" ). Suppose one has a small hotel,
then he may write many programs, e.g. one for reservation, one for
inspecting the status of the rooms (which are vacant, which are
to be vacant today at noon, which are vacant and is waiting for
room-mates to clean the room, ....), one for customer's details,
one for entering laundry charges, one for entering food charges , ....
Then he may use F1, .... , F12 to "chain" to those programs, and
when finished, it may be chained back to the main program.
(Note : to pass arguments between programs, both the chained
programs and the chaining program must contain statements like
common a as long, b as double ,c as double,....)
(Here "common", like "chain" is a Qbasic keyword.)
If you can make good use of these features, you can really do lot of data processing work with you PC.
In the Chapter about random file, we have discussed data types, and user defined data types, e.g.
type newtype
aa as integer
bb as long
cc as string*40
dd as single
end type
dim uuu as newtype
And we use "uuu.aa, uuu.bb, uuu.cc" to access those variables.
It is a pity that Qbasic would not allow nesting of data types, like that shown below :
type newtype
bb as long
cc as string*40
dd as single
end type
type secondgen
pp as newtype
qq as newtype
rr as double
end type
type thirdgen
ss as newtype
tt as secondgen
zz as integer
end type
dim vvv as thirdgen
And use " vvv.zz, vvv.ss.bb, vvv.ss.cc, vvv.ss.dd, vvv.tt.pp.cc, vvv.tt.qq.dd .... " to access those variables.
C permits nesting of data types. (Note : In C , data type is called data structure.)
WINDOW , as well as many microsoft's commercial softwares, use lots of nested data type (or nested data structures). "Objects" need parameters, and to set parameters to objects, one selects (click and drag to highlight) the object first, then press right-button of mouse, and then one may set properties. These properties are really data for data structures.
Ever since computer was invented, "linear sequence of execution of statements " is the staying feature of computer.
Present day trend is on "object" programming and clicking icons,
which is the same as "Interrupt processing" (similar to "on ... gosub ..." in Qbasic).
There are situations where such features
are very useful, e.g. in graphical packages, in design ,.... In fact, many
human activities are random in nature.
But there are situations where such features are undesirable, e.g.
in filling forms. (Hence completely forsaking good programming practices of
years past, and make a complete switch to click-and-execute, event driven programming,
is wrong.)
In the previous chapter on Data Base, there is a "re-input" subroutine, where one may make changes. The fields are scanned one by one, and if that field needs no change, user just press "Enter". And if changes is need, he enters data then press "Enter".
Linear sequence and ordering give one peace of mind.
Visual Basic forsakes "linear sequence of execution", and favours random execution. Hence to accomplish the above, the programmer has to do lot of extra programming (get focus, setting tab index , ...), simply to enter data into forms!
Reliability should be the main consideration. If a program has glitches, would you entrust that to do accounting, scientific research for you? We demand 100% accuracy in scientific processing, and 100% in financial matter in business. Hence I do not think Visual Basic is superior than Qbasic in this aspect. I just wish one day some programmer would write a 32 bit Qbasic compiler, stripping away many features of history (from these 12 chapters, we can see we can accomplish very much with very little features.) but make them absolutely reliable and easy to use, and also allows us to use library, as in C.
For details, please see online manual.
DEF FNname(... as ... , ... as ... , ....)
..... [EXIT DEF]FNname = expression
.....END DEF
DEFLNG I-N
DEFDBL A-H, O-Z
DIM kk(1000)
RANDOMIZE TIMER
FOR i = 1 TO 1000
kk(i) = INT(RND * 5000)
PRINT kk(i);
NEXT i
PRINT
END
Here " randomize, timer, rnd, int( ), mod " are all
Qbasic key words, built-in functions.
It tells Qbasic that array element starts from 0,option base 1
e,g. a(0), a(1) ,...
It tells Qbasic that array elements starts from 1,dim a(4 to 100)
e.g. a(1), a(2) ,....
dim a(10)
for i = 1 to 5
read a(i)
print a(i);
next i
print
data 60,70,85,68,45
We have come to the end of Qbasic lessons. I hope you would find the material useful. If you find errors, or have idea about improvement, or know of better ways of doing thing, or ..... kindly email me at "sywu@pacific.net.hk". I am not sure I will receive your letter, because some bad people are trying to isolate me. I am even not sure that the lessons I prepare may appear in the Internet!
| [Previous] | [Home] |