Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.misc > #2140
| From | "James Harris" <james.harris.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.misc |
| Subject | Re: 100 Things |
| Date | 2014-05-02 11:11 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <ljvr0j$cgl$1@dont-email.me> (permalink) |
| References | (4 earlier) <K278v.218753$vB1.137675@fx36.am4> <ljria6$8ka$1@dont-email.me> <85e8v.227832$gV7.159905@fx21.am4> <ljs1cm$kig$1@dont-email.me> <Nsq8v.316223$%n2.142416@fx30.am4> |
"BartC" <bc@freeuk.com> wrote in message
news:Nsq8v.316223$%n2.142416@fx30.am4...
> "James Harris" <james.harris.1@gmail.com> wrote in message
> news:ljs1cm$kig$1@dont-email.me...
>> "BartC" <bc@freeuk.com> wrote in message
>> news:85e8v.227832$gV7.159905@fx21.am4...
>>> "James Harris" <james.harris.1@gmail.com> wrote in message
>
>>> But how would C's for-loop help here? Surely you still have to choose
>>> between a <= or >= terminating condition?
>>
>> C's for loop allows the programmer to explicitly select <, <=, > or >. 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.
>
> Pretty much every language with a conventional (non-C) 'for' statement
> will
> have a way to iterate up or down. I use:
>
> for i:=A to B do Step +1
> for i:=A downto B do Step -1
> for i:=A to B by C do
> for i:=A downto B by C do
>
> With the 'by' forms expecting C to be a constant (either positive or
> negative; it will make 'to' into 'downto' as needed). (If not, it is
> assumed
> to be positive or negative respectively).
>
> (BTW the 'downto' method is from Pascal, which I thought was a good
> simplification in forcing a loop direction to be known at compile-time.)
If "to" always meant "upto" and there was a separate "downto" the direction
issue is resolved. Of course, the word "to" doesn't sound like it is always
ascending and IIRC a Basic allowed "to" to step downwards as in
for P = 10 to 1 step -1
But if "to" can mean either direction depending on the use of "by" that
sounds a little complicated!
>> 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 >=.
>
> This is easy. You just don't worry about it. Or prohibit it when the
> compiler can't figure out what to do.
Umm, not sure about that. Consider
S = function_call();
for P = Q to R step S;
Can the compiler figure out what to do? Probably not. Now compare that with
S = -1;
for P = Q to R step S;
Can the compiler figure out here what to do? Probably yes but only if it
does data flow analysis. Yet the statement in question is the same in each
case. I think your idea of defining the direction by a keyword is *much*
better than stating that the step controls the direction.
>> 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?
>
> (Have you ever looked at the code you've written, and counted how many
> times
> you've used 'for', and what proportion use simple +1 or -1 iteration, and
> what proportion used anything more complex?
Sorry, I cannot say I have. So ... I have pulled out the following from the
only significant piece of C I have worked on recently.
$ cat sformat.c | grep "^ *for"
for (; padding_reqd-- > 0;) *p++ = pad_char;
for (i = 0; i < qspaces; i++) *dest++ = ' ';
for (i = 0; i < qzeroes; i++) *dest++ = '0';
for (i = 0; i < qdigits; i++) *dest++ = *p++;
for (i = 0; i < qspaces; i++) *dest++ = ' ';
for (i = 0; i < qzeroes; i++) *dest++ = '0';
for (i = 0; i < qdigits; i++) *dest++ = *p++;
for (i = 0; i < qspaces; i++) *dest++ = ' ';
for (i = 0; i < qzeroes; i++) *dest++ = '0';
for (i = 0; i < qdigits; i++) *dest++ = *p++;
for (; (ch = *f++);)
for (more = 1; more;)
for (;;)
for (;;)
for (more = 1; more;)
You can see a mixture in there. Some use your standard up paramaters, one
counts down, one loops until it reaches a zero byte, two use a "more" flag
and two others simply loop.
If you wanted to see a 90+% run of standard up or down parameters I bet you
are disappointed! ;-) In reality this is just a sample piece of code and is
not meant to be typical. I might use the standard up or down loops a lot
more overall. The above forms just suited the code I was writing at the
time.
As programmers we get in to the mindset of writing code in a certain way and
tend to adapt the problem to the way our minds work. You maybe use the form
you do so much because you have become accustomed to thinking in that way.
Similarly, I expect I use C's for mechanism in the way I do because I have
got used to that approach.
We started this subthread because you expressed surprise that few languages
had added anything simpler to C's for loop. I have tried to explain why I
think that C's approach is so good that it doesn't need the addition you
would like, and why that addition can bring with it a lot of unwanted
baggage.
My own loop preference is not C's but I still think C's form brilliantly
simplifies some of these long-standing issues. Because of that and because
it subsumes the type you prefer I am not surprised that other langages have
copied C and not added another form.
> You might be overestimating the number of times you need a more complex
> loop.)
When programming in C I don't think of the C for loop as being complex; it
seems quite natural to use.
In Python, however, I often write things like
for record in records:
...
Thanks for your comments on my nextof call (snipped).
James
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