Xref: utzoo misc.misc:5926 comp.misc:5942 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!uwvax!tank!eecae!cps3xx!rang From: rang@cpsin3.cps.msu.edu (Anton Rang) Newsgroups: misc.misc,comp.misc Subject: Re: The "evil" GOTO (Was: 25 Years of BASIC) Message-ID: <2854@cps3xx.UUCP> Date: 5 May 89 02:06:16 GMT References: <1814@ubu.warwick.UUCP> <13113@lanl.gov> Sender: usenet@cps3xx.UUCP Reply-To: rang@cpswh.cps.msu.edu (Anton Rang) Organization: Michigan State University, Computer Science Dept. Lines: 47 In-reply-to: jlg@lanl.gov's message of 4 May 89 22:38:12 GMT In article <13113@lanl.gov> jlg@lanl.gov (Jim Giles) writes: > Actually, in the above example, "break" is a better way. This is the > classic "loop and a half" problem for which the "break" statement > exists. So the following is a better suggestion: > > printf ("Enter your sex: "); > for (;;) { > gets (sex); > if (sex == "m" || sex == "f") break; > printf (" or only: "); > } > > Note: since C doesn't have multi-level breaks, GOTO must be used to escape > from nested constructs. This is ugly; it essentially transforms the code LABEL: get input if (bad input) print message go back to label into LABEL: get input if (good input) goto LABEL2 print message go back to label LABEL2: Now you have two places where control jumps around, instead of just one. 'break' has its uses, perhaps, but this isn't one of them (IMHO). Actually, I feel that 'break' should usually be replaced by an explicit goto (heresy!). Why? Because the place the break goes to isn't explicit; you have to start matching up loops. If you have lots of nesting, this is quite a mess. Named loops in Ada are intended to alleviate this problem, and if used properly seem to do a decent job. +---------------------------+------------------------+-------------------+ | Anton Rang (grad student) | "VMS Forever!" | VOTE on | | Michigan State University | rang@cpswh.cps.msu.edu | rec.music.newage! | +---------------------------+------------------------+-------------------+ | Send votes for/against rec.music.newage to "rang@cpswh.cps.msu.edu". | +---------------------------+------------------------+-------------------+