comp.lang.ada
 help / color / mirror / Atom feed
From: "David Thompson" <david.thompson1@worldnet.att.net>
Subject: loop statements, was Re: Local vs global variables in ADA
Date: Sun, 10 Nov 2002 01:36:11 GMT
Date: 2002-11-10T01:36:11+00:00	[thread overview]
Message-ID: <fKiz9.10351$SY3.579899@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: u3cqelhzw.fsf@gsfc.nasa.gov

Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov> wrote :
> Robert A Duff <bobduff@shell01.TheWorld.com> 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








  reply	other threads:[~2002-11-10  1:36 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-03 18:17 Local vs global variables in ADA Roger
2002-11-03 19:03 ` Per Sandbergs
2002-11-03 19:31 ` Eric Jacoboni
2002-11-04  1:47 ` Jeffrey Carter
2002-11-04 13:27   ` Wes Groleau
2002-11-04 20:48     ` Jeffrey Carter
2002-11-05  8:43       ` Fraser Wilson
2002-11-05 13:53         ` Charles H. Sampson
2002-11-06  4:59           ` R. Tim Coslet
2002-11-05 17:28         ` Stephen Leake
2002-11-05 17:38           ` Jean-Pierre Rosen
2002-11-05 19:57             ` Jeffrey Carter
2002-11-06  9:11               ` Jean-Pierre Rosen
2002-11-05 20:26             ` Vinzent Hoefler
2002-11-05 23:14             ` Wes Groleau
2002-11-06  9:16               ` Jean-Pierre Rosen
2002-11-06 13:49                 ` Wes Groleau
2002-11-05 22:37           ` Robert A Duff
2002-11-05 23:46             ` Larry Hazel
2002-11-06  2:19               ` Dennis Lee Bieber
2002-11-06 13:45                 ` Dan Nagle
2002-11-07  0:30                   ` Dennis Lee Bieber
2002-11-06  2:15             ` Dennis Lee Bieber
2002-11-06  7:04               ` Martin Dowie
2002-11-06 14:40               ` john mann
2002-11-07  0:25                 ` Dennis Lee Bieber
2002-11-07 14:58                   ` Robert A Duff
2002-11-07 15:38                     ` Jean-Pierre Rosen
2002-11-08  1:43                   ` Jeffrey Carter
2002-11-06  7:18             ` Dale Stanbrough
2002-11-06 15:40             ` Rod Chapman
2002-11-06 17:34             ` Stephen Leake
2002-11-10  1:36               ` David Thompson [this message]
2002-11-05 19:54         ` Jeffrey Carter
2002-11-04 13:47 ` Martin Dowie
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox