Re: More Wish List
- Posted by Derek Parnell <dparnell at BIGPOND.NET.AU> Sep 30, 2000
- 418 views
The [continue] statement has got my vote too. The [else] statement seems like a good idea to handle exception conditions, however, what is an exception? I normally use the [exit] when an exception happens. In other words, in my mind, a loop finishes *normally* when the exit is *not* used. So I would prefer something more like ... for i = 1 to length( s ) do ... code goes here ... if test1 then exit code1 end if ... more code can go here ... if test2 then exit code2 end if ... more code can go here ... when code1 exception processing ... when code2 exception processing ... end for This type of syntax was suggested by Donald Knuth in his paper "Structured Programming with go to Statements", 1974. Yes, it is disguised goto. Just as [exit] and [continue] are. But they are all controlled gotos. That is the problem with the plain goto statement - it is uncontrolled. I also refer the interested reader to the following articles... "Go To Statement Considered Harmful" by E. Dijkstra, 1968 "The Translation of 'go to' Programs to 'while' Programs" by E. Ashcroft and Z. Manna, 1972 "A Case Against the GOTO" by W. A. Wulf, 1972 "A Case for the GOTO" by M.E. Hopkins, 1972 "On the Composition of Well-Structured Programs" by N. Wirth, 1973 "Programming Style: Examples and Counterexamples" by B W Kernighan and P J Plauger, 1974 as you can see, this debate (read: "religious war") has been raging for quite a long time. ---- cheers, Derek ----- Original Message ----- From: "Cuny, David at DSS" <David.Cuny at DSS.CA.GOV> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Saturday, September 30, 2000 5:56 AM Subject: More Wish List > Here's a wish for the 'while' and 'for' loops: > > [for loop] > > for <var> = <start> to <end> [by <step>] do > [exit] > [continue] > [else > <statements>] > end for > > [while loop] > > while <test> do > <statements> > [exit] > [continue] > [else > <statements>] > end while > > [continue] > > This instruction causes the code to jump to the top of the loop. It's > standard with most languages, and always puzzled me that Euphoria hasn't got > one yet. > > [else] > > I found this one in Python, and it's pretty cool. If the loop is exited > *without* encountering an 'exit' statement, the 'else' branch is taken. So > instead of having to write this sort of thing to handle exceptions: > > integer found > > found = False > for i = 1 to length( s ) do > ... code goes here ... > if test then > flag = True > exit > end if > end for > > if not found then > ... exception processing ... > end if > > you could use the 'else' to handle the exception: > > for i = 1 to length( s ) do > ... code goes here ... > if test then > exit > end if > else > ... exception processing ... > end for > > It not only makes the code shorter, but it binds the exception code to the > loop construct, which is where it logically belongs. > > -- David Cuny