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


Groups > comp.lang.c > #12648

Re: the loop control

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: the loop control
Date 2011-11-04 00:43 +0000
Organization A noiseless patient Spider
Message-ID <20111103203052.351@kylheku.com> (permalink)
References <j8hrok$3cq$1@reader1.panix.com> <op.v35g1ubrlahckh@cantereyd> <j8miq2$kh4$1@reader1.panix.com> <20111031152454.835@kylheku.com> <j8v134$3ug$1@reader1.panix.com>

Show all headers | View raw


On 2011-11-03, Joe keane <jgk@panix.com> wrote:
> I use it all the time.
>
> The idiom
>
>     while (--ct >= 0)
> 	...

This is not an idiom; the meaning of this construct is exactly what
the rules for that combination of symbols say it is.

> has the nice property that if ct is negative coming in [almost], the

Almost, yes. What if it is INT_MIN already?

It also has the property that if ct is zero, it is not executed,
but you didn't catch that because it's not obvious, see below.

> body is not executed, which is probably what you want.

This has the stupid property of testing a value that is not the one
which came in. This makes it awkward to think about.

For what values of ct is the loop executed?  It is ct - 1 which is tested, not
ct. 

  while (ct - 1 >= 0)

which is the same thing as

  ct >= 1

which is the same as

  ct > 0

Aha! Well, doh, why don't you use THAT condition in the fist place???

  while (ct-- > 0) {
  }

This still has the property of not executing the loop if ct is negative
coming in. It's algebraically equivalent, get it?

Furthermore, maybe you don't want to unconditionally decrement the variable,
whether or not the loop body executes!

If ct comes in as 0 is it okay to make it -1? That's fine if ct has no 
next use after the loop.

Variations on the following are generally better:

  for (; ct > 0; ct--) {

  }

The loop guard controls the loop AND the increment step, which is part
of the loop.

This still has the virtue that the loop is not executed when ct comes
in as zero or negative. Furthermore, it has the virtue that ct is not
touched either if it is zero or negative. (Furthermore, it won't
decrement down from INT_MIN.)

> And similar:
>
>     for (j = upp; j < low; j++)
> 	...

This is not similar at all.

> If you need unsigned for a loop counter, there must be some reason for
> doing so.

Like maybe it occurs in code written by someone else you're maintaing?

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


Thread

the loop control jgk@panix.com (Joe keane) - 2011-10-29 21:38 +0000
  Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-10-30 10:48 +1300
  Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-29 22:10 +0000
    Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 15:21 +0000
      Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 11:35 -0400
        Re: the loop control jgharston <jgh@arcade.demon.co.uk> - 2011-10-31 10:27 -0700
          Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 18:36 +0000
          Re: the loop control Robert Wessel <robertwessel2@yahoo.com> - 2011-10-31 13:49 -0500
          Re: the loop control Sjouke Burry <s@b> - 2011-10-31 18:49 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:07 -0400
          Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 13:56 -0700
      Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 17:18 +0000
        Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-01 04:35 -0700
          Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
            Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 01:16 -0700
      Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:23 +0000
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 04:47 +0000
      Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:07 +0200
        Re: the loop control Ike Naar <ike@sverige.freeshell.org> - 2011-11-01 00:11 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 22:01 -0400
            Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-01 02:51 +0000
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:23 -0400
            Re: the loop control Ike Naar <ike@iceland.freeshell.org> - 2011-11-01 09:51 +0000
              Re: the loop control pete <pfiland@mindspring.com> - 2011-11-01 05:34 -0500
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:30 -0400
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:35 -0400
            Re: the loop control Hans Vlems <hvlems@freenet.de> - 2011-11-11 00:38 -0800
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 03:36 +0000
          Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-11-02 17:09 +1300
            Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-11-02 03:56 -0700
      Re: the loop control Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-31 21:38 +0000
  Re: the loop control Acid Washed China Blue Jeans <chine.bleu@yahoo.com> - 2011-10-29 19:31 -0700
  Re: the loop control Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-10-29 23:11 -0600
  Re: the loop control "Philip Lantz" <prl@canterey.us> - 2011-10-29 22:47 -0700
    Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 16:36 +0000
      Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:30 +0000
        Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 12:38 -0700
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:51 -0400
            Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 13:06 -0700
            Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 14:17 -0700
              Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
            Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:30 +0200
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-03 21:29 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-03 18:00 -0400
          Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 00:43 +0000
            Re: the loop control jgk@panix.com (Joe keane) - 2011-11-11 02:24 +0000
      Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:20 +0200
      Re: the loop control pete <pfiland@mindspring.com> - 2011-10-31 18:01 -0500
        Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-02 01:20 +0200
  Re: the loop control 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-02 01:25 -0700

csiph-web