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


Groups > comp.lang.c > #150334

Re: After doing a lot of the problems on codingbat.com

Subject Re: After doing a lot of the problems on codingbat.com
Newsgroups comp.lang.c
References <2f146b5c-e025-46ef-af7b-7c328c9f4206@googlegroups.com> <20200227200243.157a5e51fe7577f22aa37378@g{oogle}mail.com> <b0f58500-b77b-4bf1-b390-ac69a3e3cce0@googlegroups.com> <66Y5G.466396$Uz.23163@fx32.am4> <6e7cb40b-afaa-4c54-b445-6cbf407c0697@googlegroups.com>
From Bart <bc@freeuk.com>
Message-ID <T596G.615708$cW.132364@fx16.am4> (permalink)
Organization virginmedia.com
Date 2020-02-28 14:04 +0000

Show all headers | View raw


On 28/02/2020 00:40, James Kuyper wrote:
> On Thursday, February 27, 2020 at 6:17:31 PM UTC-5, Bart wrote:

>>       unsigned mask = 0x8000h
>>       while (mask) {
>>           body;
>>           mask >>= 1;
>>       }
>>
>>       Node * node = head;
>>       while (node) {
>>           body;
>>           node = node->next;
>>       }

> Frankly, the only reason I can imagine for finding the for() versions
> more confusing is a mindset that insists on interpreting them as
> equivalent to Fortan FOR loops, with unnecessary extra capabilities. If
> you instead think of them as a convenient repacking of common patterns in
> while loops, they are much easier to understand.

Fortran loops are a good example. They are necessarily limited in what 
they do, and that makes it very easy to reason about compared with the 
myriad variations that can be introduced with C's for as I showed in my 
other post.

Actually, modern Fortran doesn't seem to have needed to adopt anything 
like C's 'for' either; it has DO, and it has DO WHILE.

I did try to duplicate it once (with an eye to easily translate C code), 
but it used WHILE to make it clear it wasn't FOR:

   while i=0, i<N, ++i do ...

But it was never used and quickly dropped. I just didn't like combining 
statements like this, which inevitably end up on the same line, and 
which can be out-of-order (deferred execution).

(Write such a loop on paper, and then with a red pen show the flow of 
control; it will be a mess, with three entry points into the loop header.)

Maybe there is an advantage to encapsulating the increment part (to 
solve the issues with 'continue' for example), but in a true 'for' loop, 
there is NO increment part; the idea is that the compiler fills in those 
details!

(If I wanted to isolate the incrementing part of a while loop like the 
above, there is a way but it is not tidy [not C syntax]:

     i := 0
     while i<N do
         to 1 do
             body...
         od
         ++i
     od

Here at least the increment is in the right place.

There can be an advantage also to encapsulating the initialisation code 
in a while loop, but not in C. I could use that because I have a 
'restart' loop control, which is not available in C. But even that is 
possible with an extra outer loop.

Meanwhile many of my loops now look like this: 'for i in A do'; where is 
the initialisation, where is the condition for end of loop, and where is 
the increment?)

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


Thread

After doing a lot of the problems on codingbat.com chad <cdalten@gmail.com> - 2020-02-25 06:55 -0800
  Re: After doing a lot of the problems on codingbat.com Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-02-27 20:02 +0300
    Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 11:41 -0800
      Re: After doing a lot of the problems on codingbat.com Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-02-27 14:48 -0700
      Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-27 23:17 +0000
        Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 16:40 -0800
          Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 01:08 +0000
            Re: After doing a lot of the problems on codingbat.com Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:37 -0800
            Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 06:21 -0800
              Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 16:15 +0000
                Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 10:37 -0800
                Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 19:22 +0000
                Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-03-01 14:54 +0100
                Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-03-01 15:36 +0000
                Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-03-01 17:08 +0100
          Re: After doing a lot of the problems on codingbat.com Richard Damon <Richard@Damon-Family.org> - 2020-02-27 21:57 -0500
          Really??? (Was: After doing a lot of the problems on codingbat.com) gazelle@shell.xmission.com (Kenny McCormack) - 2020-02-28 13:06 +0000
            Re: Really??? (Was: After doing a lot of the problems on codingbat.com) chad <cdalten@gmail.com> - 2020-02-28 05:47 -0800
            Re: Really??? (Was: After doing a lot of the problems on codingbat.com) James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 05:59 -0800
          Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 14:04 +0000
            Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-28 17:50 +0000
              Re: After doing a lot of the problems on codingbat.com Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-02-28 20:59 +0300
                Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-28 22:37 +0000
              Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-29 00:17 +0000
                Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-29 01:01 +0000
        Re: After doing a lot of the problems on codingbat.com Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:22 -0700
    Re: After doing a lot of the problems on codingbat.com Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-02-27 14:52 -0500
      Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 20:42 +0000
        Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-02-27 22:59 +0100
      Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-27 20:48 +0000
      Re: After doing a lot of the problems on codingbat.com Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 20:06 -0700
    Re: After doing a lot of the problems on codingbat.com chad <cdalten@gmail.com> - 2020-02-27 15:28 -0800

csiph-web