Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.misc > #2102
| From | Andy Walker <news@cuboid.co.uk> |
|---|---|
| Organization | Not very much |
| Newsgroups | comp.lang.misc |
| Subject | Re: 100 Things |
| References | (3 earlier) <ljqrp2$pli$1@dont-email.me> <K278v.218753$vB1.137675@fx36.am4> <ljria6$8ka$1@dont-email.me> <85e8v.227832$gV7.159905@fx21.am4> <ljs1cm$kig$1@dont-email.me> |
| Message-ID | <JZo8v.62500$FD6.39366@fx10.fr7> (permalink) |
| Date | 2014-05-01 11:03 +0100 |
On 01/05/14 00:35, James Harris wrote:
> "BartC" <bc@freeuk.com> wrote in message
> news:85e8v.227832$gV7.159905@fx21.am4...
[Not really the topic of this article, but I was (pleasantly)
surprised by how many of Bart's 115 Things I agreed with! Anyway ...]
> C's for loop allows the programmer to explicitly select <, <=, > or >.
Or, in other words, *forces* the programmer explicitly to select
[or, of course, to supply some other Boolean construct or (empty)], with
a decent chance of a resultant bug.
> In
> what I understand to be your preferred syntax, though, there is nowhere to
> put the <= or >= terminating condition and there's nothing there to tell the
> compiler whether X is supposed to increase from A to B or to decrease from A
> to B. When the step is a signed variable or an expression that could be
> positive or negative and when the semantics of the language state that the
> sign of the step controls the direction of iteration how can the compiler
> generate code for the loop termination test? It needs to know the direction
> so that it knows whether to code the loop test as, say, <= or >=.
Yes, that's the sort of thing that compilers do. But since a large
majority of loops in practice are incrementing by 1, a large majority of the
others are decrementing by 1, and a large majority of the few left over are
incrementing/decrementing by a compile-time constant, you're talking about
less than 1% of loops for which the test is slightly complicated, and so
performance takes a tiny hit. That same hit is still there in C; it's
inherent in not knowing at compile-time which way the loop will run [and
so the programmer having to write in that complexity "by hand"].
> Alternatively if the language design is that the direction of the loop is to
> be controlled by the relative values of A and B rather than the step the
> compiler still has the same problem. It doesn't know whether X should ascend
> or descend and, hence, it doesn't know whether to go on while X <= B or to
> go on while X >= B. Poor compiler! I think it would have no choice but to
> write code for both tests and select between them at run time. Am I wrong?
No; that's what compilers do. See above!
> By contrast I meant that in C one could write
> for (i = A; i <= B; i += C) /* for an ascending loop */
> or
> for (i = A; i >= B; i -= C) /* for a descending loop */
> and the job is done. Then there is no ambiguity over whether the loop
> variable is expected to increase or decrease.
Even when you don't know the sign of C? That way lie bugs!
But in any case, if you want that degree of control, C already has
the "while" loop. The problem, such as it is, with C's loops is the
existence of *three* different loop syntaxes when only one is needed,
while still not having a proper "loop-and-a-half" syntax [beyond what
can be done with "break" and "continue"].
> The effect in C doesn't need
> to be documented or remembered. What the programmer intends can be expressed
> clearly in the source code.
Not really. It's only "clear" because C has acquired various
standard forms, so that "everyone knows" what "for (i = 0; i < n; i++)"
means, and we [with any luck] no longer get obscure bugs by writing
"i <= n" or by changing the value of "n" or "i" within the loop. It's
easy to forget how hard programming is to a beginner, or when you first
use a language which has made different choices from the language(s)
you are familiar with.
[...]
>>> Further, a loop can be controlled by all kinds of things other than a
>>> stepping integer if that's what the programmer wants: input, lists,
>>> pointers, even floats can make sense with the < or <= qualifiers where
>>> they would be inappropriate for the A to B form.
>> Sure. I'm not saying discard this complex loop completely and just have a
>> basic iterative loop. I'm saying have the basic forms too for the simple
>> cases: 'for', 'to' and 'do'.
For loops other than with a stepping integer, you can, in all
sensible languages, use "while" instead:
while read (x); x > 0 do print (sqrt(x)) od;
while ptr /= 0 do { whatever }; ptr := ptr.next od
and so on. Horses for courses!
>> Syntax is free!
> Syntax is free [...].
No it isn't. It's very cheap in compilation time, but that is
very cheap anyway these days. It's not free in complexity of manuals
or syntax charts or however else people get used to your language, and
it's not free in design time if people have to deliberate [eg, in C]
over the choice between "for", "while" and "do" -- *especially* if [as
in C] the choice is not that important.
> But consider: if a language includes the simpler form then its designer has
> to deal with and document its semantics for the age-old issues mentioned
> above and earlier. A given programmer may choose not to use the simpler form
> at all but if he or she has to read someone else's code the programmer might
> still need to learn the complex and somewhat arbitrary rules that the
> language applies to those loops.
This seems a bit strained! How long does it take to learn that
"for i to n do stmt od" will evaluate "n" once and will obey "stmt" with
"i" set successively to 1, 2, 3, ,,,, n [and not at all if "n" is zero or
negative], whereas "while expr do stmt od" will repeatedly evaluate "expr"
and obey "stmt" for as long as "expr" returns the value "true"? Now your
turn to explain the allegedly less complex and arbitrary rules that C has
for "for", "while" and "do" in less than four lines.
--
Andy Walker,
Nottingham.
Back to comp.lang.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
C: 100 Things "BartC" <bc@freeuk.com> - 2014-04-29 17:57 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 07:40 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 12:16 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 13:54 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 14:39 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 20:18 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 22:40 +0100
Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-04-30 22:09 +0000
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 23:36 +0100
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 11:03 +1200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:47 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-01 00:35 +0100
Re: 100 Things Andy Walker <news@cuboid.co.uk> - 2014-05-01 11:03 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 20:16 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 12:44 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-02 11:11 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 13:32 +0100
Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-02 16:42 +0100
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 17:54 +0100
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 11:01 +1200
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-04-30 21:13 -0400
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:26 +0100
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 13:42 +1200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:35 +0100
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 21:02 +1200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 10:40 +0100
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 22:06 +1200
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 11:08 +0200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 11:02 +0100
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 13:57 +0200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 15:12 +0100
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 17:18 +0200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 17:04 +0100
Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-01 18:30 +0000
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-02 08:30 +1200
Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-03 01:29 -0400
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-03 02:37 -0400
Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-03 15:39 -0400
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-03 17:30 -0400
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-04 20:05 +1200
Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-04 13:28 -0400
Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-05 07:40 +1200
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 22:00 +0200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 23:24 +0100
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-02 08:59 +0200
Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 09:53 +0100
Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-02 11:22 +0200
Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-01 18:26 +0000
Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-02 02:49 +0000
Re: C: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-04-30 22:08 -0400
Re: C: 100 Things "BartC" <bc@freeuk.com> - 2014-07-25 23:53 +0100
Re: C: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-07-26 05:38 -0400
Re: C: 100 Things "BartC" <bc@freeuk.com> - 2014-07-26 13:51 +0100
Re: C: 100 Things rick.c.hodgin@gmail.com - 2014-10-25 04:48 -0700
Re: C: 100 Things "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2014-10-25 04:50 -0700
csiph-web