Xref: utzoo misc.misc:5957 comp.misc:5959 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!indri!ames!hc!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: misc.misc,comp.misc Subject: Re: The "evil" GOTO (Was: 25 Years of BASIC) Message-ID: <13205@lanl.gov> Date: 5 May 89 21:02:15 GMT References: <2854@cps3xx.UUCP> Organization: Los Alamos National Laboratory Lines: 59 From article <2854@cps3xx.UUCP>, by rang@cpsin3.cps.msu.edu (Anton Rang): >> printf ("Enter your sex: "); >> for (;;) { >> gets (sex); >> if (sex == "m" || sex == "f") break; >> printf (" or only: "); >> } > This is ugly; it essentially transforms the code > [...] > into > > LABEL: get input > if (good input) > goto LABEL2 > print message > go back to label > LABEL2: You are exactly right. I didn't say the feature was elegant, I said that "loop-and-a-half" problems were the reason that "break" was introduced into looping constructs (which occured in languages long before C was invented). The reason for "break" in "case" constructs only dmr can answer 8-). > [...] 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. I like the solution of named loops too. But Ada puts all but the first occurrance of the the label into the code rather than on the left margin. The advantage of GOTOs (if any) was that you could easily find the target of the branch by looking for the label. Consider the example on page 5-8 of the Ada standard: MAIN_CYCLE: loop -- initial statements exit MAIN_CYCLE when FOUND; -- final statements end loop MAIN_CYCLE; If the "initial" and "final" statement sequences were long and complicated the "exit" and "end loop" could be difficult to identify. (Even indentation only helps a little if the code crosses page boundaries.) In the Nemesis programming language the above loop would be rendered as: MAIN_CYCLE: do -- initial statements MAIN_CYCLE: exit if (FOUND) -- final statements MAIN_CYCLE: end do Here, no matter how long and complicated the statement sequences are, all the controls for the "MAIN_CYCLE" loop are easily identified by scanning the left margin of the code. Note that the identifier "MAIN_CYCLE" is the _name_ of a loop and _not_ a statement label (you can't GOTO MAIN_CYCLE). Loop names _must_ appear on all control statements for the loop. Unnamed loops are allowed and must _not_ have a name on any control statement.