I'm teaching myself QBasic from Schneider's book QBasic with an
Introduction to VisualBasic. Please, forget the sarcasm: I'm
a new convert to Linux and am looking for a QBasic equivalent
even as we speak.
Schneider is rather emphatic on structuring. He says:---
"There are four primary logical programming constructs: sequence,
decision, loop and unconditional branch. Unconditional branch,
which appears in some languages as GOTO statements, involves
jumping from one place in a program to another. Structured
programming uses the first three constructs, but forbids the
fourth. One advantage of pseudocode over flowcharts is that
pseudocode has no provision for unconditional branching and thus
forces the programmer to write structured programs."
and later:---
"One major shortcoming of the earliest programming languages was
their reliance on the GOTO statement. This statement was used to
branch (that is, jump) from one line of a program to another. It
was common for a program to be composed of a convoluted tangle of
branchings that produced confused code referred to as spaghetti
code. At the heart of structured programming is the assertion of
E.W.Dijkstra that GOTO statements should be eliminated entirely
since they lead to complex and confusing programs. Two Italians,
C.Bohm and G.Jacopini, were able to prove that GOTO statements
are not needed and that any program can be written using only the
three types of logic structures discussed above.
Structured programming requires that all programs be written
using sequences, decisions, and loops. Nesting of such statements
is allowed."
Well, I can see his point. I read (this interest group, I think)
that if you can't come back to a program after three weeks and
understand it, something's radically wrong. Spaghettification
wouldn't help.
On the other hand, I think I have encountered one of Schneider's rare
instances where you *have* to use a GOTO; if I'm right then it's
not all *that* rare.
I outline the instance below. Could someone with some experience
in QBasic (alright, primitive languages) tell me how to get
around the problem *without* using a GOTO? I'd be grateful.
It occurs to me that someone knows of a program that does just
this. If so, please let me know. [I'm teaching myself and have no
qualms about plagiarism, as long as I can understand what's going
on. In addition, I used to demonstrate to biology
undergraduates and therefore have no pride or shame.]
And the next time I'm in Sydney, if there's a Slug gathering,
I'd be happy to buy drinks for those who helped...
Regards,
Bill Bennett.
------------------------------------------------------
A Game of Noughts and Crosses (aka Tic-Tac-Toe)
I've divided the problem into tasks and the tasks have become
subprograms. Their names are (or should be) self-explanatory.
CLS
DIM TheBoard$(1 TO 3, 1 TO 3)
CALL SetTheBoard(TheBoard$())
CALL DisplayTheBoard(TheBoard$())
CALL TheFirstEntries(FirstPlayer, SecondPlayer, TheBoard$())
CALL Continuing(FirstPlayer, SecondPlayer, TheBoard$())
CALl TestRowsColumnsDiagonals(TheBoard$(), flagStatus, cellEntry$)
CALL ReportStatus(flagStatus, cellEntry$)
END
Consider the subprogram TheFirstEntries. This sorted out
the first X and the first 0; this was to give me some idea
of counters and procedures. I wanted to put in a contigency
trap: someone who consistently didn't choose an X or a 0. 3
incorrect responses and the program quits.
Well, the generic loop for this is:---
Select a response, if it's wrong(1)/wrong(2)/wrong(3), a flag=1
and the far end of the loop is LOOP UNTIL FLAG=1
All very well, except that this is a contingency occurring early
in the process. Even if the flag =1, the process will still
continue down the loop. I'd like to find a way (other than using
GOTO) simply to jump out of the loop, jump over the intervening
subprograms (which are all concerned with continuing after a
correct response has been received) and simply quitting.
According to Schneider, it should be possible using only
sequence, decision and loop. Perhaps even a loop is unnenessary.
I still can't see how.
----------------------------------------------------------
--
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://slug.org.au/lists/listinfo/slug