Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.misc > #2081

Re: 100 Things

From "James Harris" <james.harris.1@gmail.com>
Newsgroups comp.lang.misc
Subject Re: 100 Things
Date 2014-04-30 20:18 +0100
Organization A noiseless patient Spider
Message-ID <ljria6$8ka$1@dont-email.me> (permalink)
References <3SQ7v.155346$AI5.24849@fx32.am4> <ljq5sv$coh$1@dont-email.me> <kY48v.181432$Ey6.180503@fx24.am4> <ljqrp2$pli$1@dont-email.me> <K278v.218753$vB1.137675@fx36.am4>

Show all headers | View raw


"BartC" <bc@freeuk.com> wrote in message 
news:K278v.218753$vB1.137675@fx36.am4...

...

>> In one stroke C's for loop removes those issues and makes it manifest in
>> the syntax what steps the program will take to control the loop. It
>> reduces a loop to the essentials and it means that one compact loop
>> control statement can be used in almost all types of loop. IMO that's
>> quite brilliant! I'm not surprised if it got copied to other languages.
>
> Yes, C's for loop is quite powerful and very flexible (although so are 
> 'if'
> and 'goto'!). But it is possible to have both that, and a dedicated loop 
> for
> the 99% of situations (in my case) where I do need a for-loop to just 
> count
> from A to B.

That reminds me of some more benefits of C's for construct. In addition to 
those mentioned before if you want a step of something other than 1 your 
languages might accept a step as in

 A to B step C

but then if C can be positive or negative the compiler won't know whether 
you want the loop to execute or not. If you intend the loop to always ascend 
and C to be positive but don't know for sure that it is, for example, you 
might have to code

  if C > 0 then for X = A to B step C

Programmers would often omit the initial test of C and then because it 
didn't know whether C would be positive or negative the compiler would have 
to select different loop termination conditions dynamically - one for a 
rising step and the other for a falling step. Or perhaps the programmer 
wanted the difference between A and B to control the direction of the 
iteration and not the step. No one can know unless the syntax is changed so 
that something is added to the statement like a stepup or stepdown keyword. 
The whole thing is a terrible bundle of questions and uncertainties. C's for 
loop does away with all of them by allowing the programmer to state exactly 
what he or she wants.

Another issue is the terminating value. I remember in Basic having to write 
things like

  for i = a to b - 1

where the - 1 or something like it had to be added to modify the termination 
value. C's for loop allows a natural slight adjustment to the termination 
condition by means of < vs <=. Contrast these two

  for (i = 0; i < len; i++)
  for (i = 1; i <= len; i++)

Both of those are idiomatic. The requirement for a slight adjustment is 
common and can be expressed easily in C.

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.

Oh, and one more thing. This is related to the problems with your preference 
that I mentioned in the earlier post. Significant text has been written 
about whether the index might be modified during the course of the loop. And 
also whether the terminating value has to be constant or not. There are a 
number of permutations: loop index yes, terminating value no, neither, both 
etc. Different languages have chosen differently and their choices need to 
be documented in the language definition. These are yet more things for the 
programmer to remember. Again, C's for loop does away with all of that. The 
C code shows exactly what the programmer has chosen to be done. What you see 
in the code will be what the program is expected to do. No complex rules to 
remember. Very sweet, great design, IMO!

James

Back to comp.lang.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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