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


Groups > comp.programming > #1958

Re: repeating something n times

From Rui Maciel <rui.maciel@gmail.com>
Newsgroups comp.programming
Subject Re: repeating something n times
Date 2012-07-15 12:18 +0100
Organization Aioe.org NNTP Server
Message-ID <jtu8tl$ucv$1@speranza.aioe.org> (permalink)
References <repeat-20120705210751@ram.dialup.fu-berlin.de> <jt7jdv$247$1@dont-email.me> <b73472b3-9e35-45e4-b9cf-ab8366f4e770@googlegroups.com> <jtsqtf$q73$2@dont-email.me>

Show all headers | View raw


BartC wrote:

>> Once in a blue moon you might need
>>
>> /* Islamic divorce */
>> for(i=0;i<3;i++)
>>printf("I divorce you\n");
> 
> It's not as common as loops needing the index, but it does come up often
> enough that it wouldn't hurt to have a special loop form for it:
> 
> to 4 do
> println "I divorce you"
> end

I suspect that it would be a case of "damned if one does, damned if one 
doesn't".  If that new loop type was proposed I would expect a number of 
users criticising the proposal for being redundant and a needless 
contribution for bloating a programming language which once upon a time was 
actually lean.  IIRC, some people already criticise C for having too many 
loop types.


> (although this simple case just becomes: print "I divorce you\n"*4 when
> the language allows). I don't know what it might look like in C, as the
> language doesn't like new keywords, but maybe:
> 
> repeat (4)
> printf("I divorce you");
> 
> Isn't that a lot simpler than the the thirteen tokens of
> "for(i=0;i<3;i++)" which also have half-a-dozen ways of getting it wrong
> in a way the compiler won't pick up?

I don't believe that the for() loop you presented actually poses any problem 
to any programmer, even those who are just starting out.

Nevertheless, if a repeat() loop is so desperately needed then why not make 
do with a simple macro?  With C99, the following would do quite well:

<code>
#include <stdio.h>

#define repeat(n) for(int i = 0; i < n; i++)

int main(void)
{
        repeat(3)
                printf("I divorce you\n");
        return 0;
}

</code>


Rui Maciel

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-06 21:56 +0100
  Re: repeating something n times Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-06 22:16 +0100
    Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-06 23:18 +0100
  Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 14:11 -0700
    Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-14 23:11 +0100
      Re: repeating something n times Ike Naar <ike@iceland.freeshell.org> - 2012-07-14 23:37 +0000
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 01:16 +0100
          Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 17:35 -0700
            Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:23 -0500
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:19 -0500
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 11:00 +0100
      Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 17:44 -0700
      Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:17 -0500
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 11:49 +0100
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-15 09:53 -0500
      Re: repeating something n times Rui Maciel <rui.maciel@gmail.com> - 2012-07-15 12:18 +0100
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 13:21 +0100
          Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 13:02 +0000
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 14:36 +0100
              Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 14:18 +0000
                Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 15:45 +0100
                Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 16:25 +0000
                Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 18:02 +0100
          Re: repeating something n times Rui Maciel <rui.maciel@gmail.com> - 2012-07-15 15:10 +0100
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 16:10 +0100
              Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-15 13:00 -0700
    Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 21:55 -0500
      Re: repeating something n times "io_x" <a@b.c.invalid> - 2012-07-17 20:47 +0200
        Re: repeating something n times "io_x" <a@b.c.invalid> - 2012-07-18 14:19 +0200
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-18 12:55 -0500
            Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-18 11:57 -0700

csiph-web