From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8002154d2966e1a1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-11-09 17:36:12 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed2.news.rcn.net!rcn!wn11feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <5Ldx9.3695$151.38236@weber.videotron.net> <3DC5D1B7.1060707@acm.org> <3DC6DD54.10200@acm.org> Subject: loop statements, was Re: Local vs global variables in ADA X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-Mimeole: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: Date: Sun, 10 Nov 2002 01:36:11 GMT NNTP-Posting-Host: 12.89.166.201 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1036892171 12.89.166.201 (Sun, 10 Nov 2002 01:36:11 GMT) NNTP-Posting-Date: Sun, 10 Nov 2002 01:36:11 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:30674 Date: 2002-11-10T01:36:11+00:00 List-Id: Stephen Leake wrote : > Robert A Duff writes: ... > > Like what? I mean, which languages have different definitions of > > "while" loops, and what are the differences. I thought "while" loops > > were one of the few cases where there is pretty much agreement on the > > semantics across languages. ... > At the moment, I only use Ada and C, so I don't really remember what > the issues are precisely. > > What I do remember is being confused about whether the loop body > executes once for a "false" while condition. Especially when the > "while" is at the end. > As opposed to not executing at all for a false test at the end? I've never seen *that*, but there are other anomalies, see below. > I believe different languages (I've used Pascal, C, COBOL, Fortran, > lisp, Ada) do this differently. But I could be wrong; it could be that > I could just never remember the (common) rule. > > Hmm. Checking my ANSI C manual, I see that if "while" is at the top, > the loop body is not executed if the condition is false. If "while" is > at the bottom, the loop body is executed once if the condition is > false. > Or, equivalently but clearer to me, the test is executed at the bottom of the body. > Ada only allows "while" at the top, and it has the same rule as C. > (Modern) Fortran has DO WHILE which does test at top (before), which is really just sugar for DO (forever) + IF NOT cond EXIT, in addition to the traditional arithmetic DO = other langs FOR. Early Fortran (66) had the infamous one-trip-do-loop: if you wrote an empty range e.g. DO I=1,0 it was officially undefined and some (most?) compilers would execute the body once before testing at the end, as an optimization -- especially on S/360 using BXLE. Modern Fortran requires skipping it. PL/1 has an amalgamated DO where you can write an arithmetic range (or at least sequence?) plus a boolean test at top/before in the same statement! I vaguely recall algol allowing something similar, but never used it because it was too confusing. As already noted, Pascal has WHILE cond DO body and REPEAT body UNTIL cond with the sense inverted apparently to make them more obviously different, as well as FOR. COBOL (85) is the worst, for my money. For inline loops the test always appears at the top of a PERFORM construct but you can specify either WITH TEST { BEFORE | AFTER }. This may(?) derive from earlier COBOL having *only* out-of-line loops -- PERFORM that-code-over-there UNTIL cond , or FOR ~ PERFORM blah VARYING i FROM 1 BY 2 UNTIL i > 9 -- where there is no body to be on the top or bottom of. Well, actually there's one thing worse -- perl. You can read through 20 lines of code thinking it's an imperative statement and only at the end discover it's a loop. > Anyway, I see no reason to strain my limited supply of brain cells > trying to remember this, when loop/exit/end loop covers all the cases, > and is perfectly clear. > Your choice. -- - David.Thompson 1 now at worldnet.att.net