Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!goanna!ok From: ok@goanna.oz.au (Richard O'keefe) Newsgroups: comp.lang.c Subject: Re: problems/risks due to programming language Message-ID: <2919@goanna.oz.au> Date: 27 Feb 90 04:16:20 GMT References: <1597@awdprime.UUCP> <8133@hubcap.clemson.edu> <2903@goanna.oz.au> <18033@rpp386.cactus.org> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 40 In article <18033@rpp386.cactus.org>, woody@rpp386.cactus.org (Woodrow Baker) writes: > In article <2905@goanna.oz.au>, ok@goanna.oz.au (Richard O'keefe) writes: In fact the section which follows was QUOTED by me, not WRITTEN by me. Please net.people, do take care with your attributions. I _did_ write > > (a) can't be done without introducing a goto or return, or rewriting > > the code. to which Woodrow Baker responded > Wrong. you can always set a flag within the various cases and test it at > the bottom or top of the loop. Yes this qualifies as re-writing the code. It seems very odd to say "Wrong" and then admit two sentences later that it isn't wrong at all. Look, you can program _any_ can of spaghetti you like with _one_ loop, _one_ switch, and _one_ flag variable. The goal is code that can be read and understood without too much strain; post hoc flag variables are even worse for that than undisguised gotos. In Ada, the method of quitting a loop early is the exit statement: exit_statement ::= EXIT [loop_name] [WHEN condition]; With no loop_name, it exits the innermost loop. With a loop_name, it exits the loop labelled with that loop_name. An equivalent for C would be a set of macros like loop(LoopName) break_loop(LoopName) end_loop(LoopName) which could be implemented thus: #define loop(X) { #define break_loop(X) goto X #define end_loop(X) ;X:;} This does amount to bending the syntax of C, but I think it may be an excusable case. The proper nesting of these macros can be easily checked by a simple tool, and it gives us a disciplined way of using the language we already have.