Re: [SLUG] That somewhat theoretical problem.
This one time, at band camp, Jon Biddell said: start of crappy basic program for x = 1 to some.big.number do something sensible here I assume flag is set in here) if flag = 1 exit else endif next x crappy basic program continues, with flag = 1 flag = 0 while flag = 0 do something if some-condition then flag = 1 end while this way you don't run off the end of the for loop... the local neighbourhood crax0r will be onto it in a second. -- "Fear leads to anger. Anger leads to hate. Hate leads to using (o_ ' Windows NT for mission-critical applications." //\ -- What Yoda *meant* to say, Devin L. Ganger, scary.devil.monastery v_/_ -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
|Not disputing that you need gotos in general but couldn't you just put |return(ret) where you have goto currently at the expense of more code? | |That would break the "one return point per function" rule... I think your goto solution is uglier than this arbitrary rule. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
On Mon, Feb 19, 2001 at 05:57:31PM +1100, Bill Bennett uttered: 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. What sarcasm? *evil grin* Schneider is rather emphatic on structuring. He says:--- That putting it rather mildly. "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. MUAHAHAHAHAHAHHAHAHA. Read all, or most of them before. To use goto, or not to use goto. I'm firmly in the latter camp, _except_ for one condition (and in Qbasic only): ON ERROR GOTO So, using that, you could call me a structured programmer, but then again, you haven't seen my Perl code. 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. Agreed. 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. 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... W! :-) 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 E. It's _just_ a game of TicTacToe. Did he have to sub the living hell out of it? 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. C, Perl, a few others (not sure about Basic - I __think__ it does) have: break(); Shown in a C stylee for simpilicties sake. Now, C/Perl/Python (?) treat this is the Right Way and dump themselves out of the loop. I also remember that Basic uses break to set a breakpoint, or something. Ah, well. 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. A Pre-test loop would be best: WHILE (not won) { other instructions if (moves 3) { won = 1; } } There's my stab at it. I'll look at it when i'm bored at work, and am more awake. :-) -- -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug -- Steve "I'm a sysadmin
Re: [SLUG] That somewhat theoretical problem.
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. This might be a rather obvious answer (fear so) but does QBasic have the C equivalent of an exit() function call. It simply termiantes the program then and there. It also returns an error code to the opreating system. Other than that, does Qbasic have some sort of expection mechanism that would allow you to throw an uncaught expception and terminate that way ? Sorry i've never used QBasic, but thatis how i woudl solve the problem (Assuming i understood it correctly) in c or c++. Have you considered learning one of those languages in stead ? I can recommend an excellent book on learning C++ properly, provided you have a little experience with the basic ideas of programming and dont mind reading. Jason -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
|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. All programs with GOTOs can be converted to equivalent programs without GOTOs if you are allowed to use extra state flags. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
RE: [SLUG] That somewhat theoretical problem.
'course in assembler, theyre all GOTOs: BRanch, JuMP, and so on. The exceptions are CALL and RETurn... Jill. -- Jill Rowling, Snr Des. Eng. Unix System Administrator Eng. Systems Dept, Aristocrat Technologies Australia 3rd Floor, 77 Dunning Ave Rosebery NSW 2018 Phone: (02) 9697-4484 Fax: (02) 9663-1412 Email: [EMAIL PROTECTED] Ken: |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. All programs with GOTOs can be converted to equivalent programs without GOTOs if you are allowed to use extra state flags. -- CONFIDENTIALITY NOTICE -- This email is intended only to be read or used by the addressee. The information contained in this e-mail message may be confidential information. If you are not the intended recipient, any use, interference with, distribution, disclosure or copying of this material is unauthorised and prohibited. Confidentiality attached to this communication is not waived or lost by reason of the mistaken delivery to you. If you have received this message in error, please delete it and notify us by return e-mail or telephone Aristocrat Technologies Australia Pty Limited on +61 2 9413 6300. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
On Mon, Feb 19, 2001 at 05:57:31PM +1100, Bill Bennett wrote: 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. The QBasic statement you want is EXIT DO, or possibly EXIT LOOP (my memory of such things is a little hazy...) e.g. DO ...loop stuff... If condition Then Exit Do ...more loop stuff... LOOP Of course, you could still use LOOP UNTIL condition2 if you wanted, as well as this. This way you can easily construct multiple exit points from your loop, which can be handy. There is also an EXIT FOR, and I think an EXIT WHILE. 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. And most "modern" languages have exceptions, which provide another fairly elegant way to achieve the same effect. Seriously, I know you're in the middle of learning QBasic, but you'd be much better off (IMHO) learning Python, either from _Learning_Python_ from O'Reilly, or just from the web. Python is as simple as QBasic in syntax, but more consistent and *alot* more powerful. QBasic will teach you nothing of the wonder of dictionaries/hashes... and for those into rigorously structured programming, Python is also object-oriented. Just my thoughts. -Andrew. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
Jason Rennie [EMAIL PROTECTED] writes: 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. This might be a rather obvious answer (fear so) but does QBasic have the C equivalent of an exit() function call. It simply termiantes the program then and there. It also returns an error code to the opreating system. I guess it should be said here that there is an "unbreakable" golden rule in programming style which says multiple exit points in a function is a no-no and that there should only be one exit point at the end. Like most "unbreakable" golden rules in programming, there are many cases when one should break it. The problem is that everyone has a different idea on what those cases are. Blood has been spilled in various attempts to come to an agreement. :) Multiple exit points on error conditions is probably ok, but I'm not willing to risk life or limb to defend that assertion. -- :%s/[Ll]inux/GNU\/Linux/g -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
RE: [SLUG] That somewhat theoretical problem.
Just to be difficult, what's the difference then between multiple exits and a (C) switch statement? Apart from the more elegant appearance of the case / switch, when you come out at the end you can't say for certain where you came from (other than what you were switching for!). And the assembler for it is identical to the test / jump (== GOTO). - Jill. -- Jill Rowling, Snr Des. Eng. Unix System Administrator Eng. Systems Dept, Aristocrat Technologies Australia 3rd Floor, 77 Dunning Ave Rosebery NSW 2018 Phone: (02) 9697-4484 Fax: (02) 9663-1412 Email: [EMAIL PROTECTED] -Original Message- From: Raoul Golan [mailto:[EMAIL PROTECTED]] I guess it should be said here that there is an "unbreakable" golden rule in programming style which says multiple exit points in a function is a no-no and that there should only be one exit point at the end. -- CONFIDENTIALITY NOTICE -- This email is intended only to be read or used by the addressee. The information contained in this e-mail message may be confidential information. If you are not the intended recipient, any use, interference with, distribution, disclosure or copying of this material is unauthorised and prohibited. Confidentiality attached to this communication is not waived or lost by reason of the mistaken delivery to you. If you have received this message in error, please delete it and notify us by return e-mail or telephone Aristocrat Technologies Australia Pty Limited on +61 2 9413 6300. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
RE: [SLUG] That somewhat theoretical problem.
On Tue, 20 Feb 2001, Jill Rowling wrote: Just to be difficult, what's the difference then between multiple exits and a (C) switch statement? The switch is used for programming a list of alternates. No exit/egress is involved. The next statement after the switch is always executed -- unless you've populated it with gotos and exits ;) Multiple exits are jsut that: multiple points of egress from a function/subroutine. The problem with the latter is that often there are some required exit conditions to be met (i.e. setting up return values, destroy your mess, etc) that make maintenance a nightmare if there many exit points. It is very easy (too easy) to introduce subtle bugs in subroutines that have multiple exit points. -- Rick Welykochy || Praxis Services -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
Like most "unbreakable" golden rules in programming, there are many cases when one should break it. The problem is that everyone has a different idea on what those cases are. Blood has been spilled in various attempts to come to an agreement. :) Multiple exit points on error conditions is probably ok, but I'm not willing to risk life or limb to defend that assertion. I guess i'm a sloppy programmer then, i stick exit()'s in code when I have some nasty fail error, sinply becasue it is easy to see where it was up to, and the exits live with the broken peice of code :) Jason -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
On Tue, Feb 20, 2001 at 11:47:34AM +1100, Jill Rowling wrote: Just to be difficult, what's the difference then between multiple exits and a (C) switch statement? Apart from the more elegant appearance of the case / switch, when you come out at the end you can't say for certain where you came from (other than what you were switching for!). As a switch's statement can be replaced by a heap of ifs, elses and a matched_one boolean, switch's behaviour is deterministic and it should be possible to trace the flow of control through the statement (usually a block). I'm thinking of turning up to the meeting on friday. Will there copies of potato available? What is the usual cost for these? *grumble* slow modem connection... -- Tom main(){++*(int*)0;} -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
quote who="Tom Nott" I'm thinking of turning up to the meeting on friday. Will there copies of potato available? What is the usual cost for these? *grumble* slow modem connection... I can probably whip one up for you - Anthony Rumble usually comes along with a few CDs for sale too. - Jeff -- [EMAIL PROTECTED] --- http://linux.conf.au/ -- o/~ In spite of all those keystrokes, you're addicted to vim. *ka-ching!* o/~ -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
On Tue, 20 Feb 2001 14:51:43 +1100 (EST), Jason Rennie wrote: I guess i'm a sloppy programmer then, i stick exit()'s in code when I have some nasty fail error, sinply becasue it is easy to see where it was up to, and the exits live with the broken peice of code :) Well here is one circumstance where (IMO), it is quite OK to have gotos, viz: afunction() { int ret = 0; switch(whatever) { case x: if((ret = dosomething()) == 0 ) goto fail; case y: if((ret = dosomethingelse()) == 0) goto fail; } fail: return(ret); } gotos should be used when they make sense, I reckon they definately have their place. Regards -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] That somewhat theoretical problem.
On Tue, 20 Feb 2001 17:30:50 +1100, Ken Yap wrote: Not disputing that you need gotos in general but couldn't you just put return(ret) where you have goto currently at the expense of more code? That would break the "one return point per function" rule... Regards -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
[SLUG] A somewhat theoretical problem.
Would anyone mind if I posted a problem in programming? I'm teaching myself from a book and although I can get my program to work, the book says that I've used something illegal. (Yes, There *is* an obvious reply...) I should add that the problem is not confined to the language I'm learning and, I presume, has been discussed ad infinitum wherever programmers raise their glasses. Even so... Regards, Bill Bennett. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] A somewhat theoretical problem.
Bill Bennett wrote: Would anyone mind if I posted a problem in programming? Obviously if it is a programming language that is used in Linux, like C, etc, there shouldn't be any problems. If there is one thing on this list that gets a good thread, it is usually a question about scripting/programming. Mind you, some of the answers are not that useful, but interesting nevertheless Now, if it is an MS language like Visual Basic, Vis C, etc, you have to be joking or have a death wish {:-). Hmm, isn't there Rexx for Linux somewhere? -- Terry Collins {:-)}}} Ph(02) 4627 2186 Fax(02) 4628 7861 email: [EMAIL PROTECTED] www: http://www.woa.com.au WOA Computer Services lan/wan, linux/unix, novell "People without trees are like fish without clean water" -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
Re: [SLUG] A somewhat theoretical problem.
On Mon, 19 Feb 2001, Bill Bennett wrote: I'm teaching myself from a book and although I can get my program to work, the book says that I've used something illegal. (Yes, There *is* an obvious reply...) Unable to comply. Please post entire error message, machine name, operating platform, language name, sample code and free sample(s). -- Rick Welykochy || Praxis Services -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug
[SLUG] That somewhat theoretical problem.
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
Re: [SLUG] That somewhat theoretical problem.
Bill Bennett wrote: I'm teaching myself QBasic from Schneider's book QBasic with an Introduction to VisualBasic. Please, forget the sarcasm: Okay, Questions like this belong on the Slug-Chat list. .snip. 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. what exactly can you not see? And what was your question? Hint, programming text writers often write crap for which the answer is so trivial or they suddenly redefine stuff, or their answer (if they give it) doesn't meet their criteria. After learning Basic, Fortran, Algol, Cobol, Lisp, Pascal and C, I've seen this repeatedly. In O X's you only need to call nine moves, alternating white/black/w/b/w and commence testing after white has made three moves. In a non-loop solution, Successive calls (4th, 5th, etc) start with a test to see if won flag has been set. In a loop colour=white won=no while not won do call move test for win change colour done annouce winner If this is a for marks assignment, look at the Scientific American http://www.sciam.com/ site. I suspect they will have quite a few analysises (sp?) of the maths behind it all -- Terry Collins {:-)}}} Ph(02) 4627 2186 Fax(02) 4628 7861 email: [EMAIL PROTECTED] www: http://www.woa.com.au WOA Computer Services lan/wan, linux/unix, novell "People without trees are like fish without clean water" -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://slug.org.au/lists/listinfo/slug