Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

while( ) and test condition

0 views
Skip to first unread message

nembo kid

unread,
Sep 4, 2008, 6:11:33 AM9/4/08
to
If i>0 the while loop is executed; if i==0 not.

Ok, but also if i<0 the while loop is executed.

So, which are the rules? Which values must assume the "test condition"
to be assumed like true o false? Thanks in advance


#include <stdio.h>

int main( void )
{

int i = 0; /* while loop isn't executed */


while (i)

{
printf ("i'm while-looping...Ctrl-C to exit");
}


}

pete

unread,
Sep 4, 2008, 6:14:32 AM9/4/08
to

while (i)
means the exact same thing as
while ((i) != 0)

Therefore:
while (i>0)
means the exact same thing as
while ((i>0) != 0)


and:
while (i<0)
means the exact same thing as
while ((i<0) != 0)

--
pete

Richard Bos

unread,
Sep 4, 2008, 6:16:12 AM9/4/08
to
nembo kid <nembo@kid> wrote:

> So, which are the rules? Which values must assume the "test condition"
> to be assumed like true o false? Thanks in advance

_All_ logical conditions in C, regardless of whether they're in a while
loop, a for loop, an if statement, or wherever, are false if they
compare equal to 0, and true in all other cases.
(Note: compare equal to 0 need not mean "is an integer and has the value
zero". For example, you can put a pointer expression in an if statement,
and it will trigger if the pointer is not a null pointer.)

Richard

August Karlstrom

unread,
Sep 4, 2008, 6:30:29 AM9/4/08
to

As others have already said, zero is false and all other values are
true. Anyway, for clarity it is probably best to be explicit and do a
comparison (unless a variable or function has a boolean interpretation
already) -- we want to concentrate on real problems, not on "tricks"
within the language.


August

Sjoerd

unread,
Sep 4, 2008, 9:45:19 AM9/4/08
to
On Thu, 04 Sep 2008 12:30:29 +0200, August Karlstrom wrote:
> Anyway, for clarity it is probably best to be explicit and do a
> comparison (unless a variable or function has a boolean interpretation
> already) -- we want to concentrate on real problems, not on "tricks"
> within the language.

Actually, some people don't use an explicit comparison and on top of that
use a non-trivial expression, such as this:

while (line = fgets(buf, 10, fp)) ...

This does not compare line to the result of fgets() because there is only
one = sign. Instead, this first assigns the result of fgets() to line and
then compares it to 0 to see if the loop should be entered.

August Karlstrom

unread,
Sep 4, 2008, 10:01:16 AM9/4/08
to
Sjoerd wrote:
> Actually, some people don't use an explicit comparison and on top of that
> use a non-trivial expression, such as this:
>
> while (line = fgets(buf, 10, fp)) ...
>
> This does not compare line to the result of fgets() because there is only
> one = sign. Instead, this first assigns the result of fgets() to line and
> then compares it to 0 to see if the loop should be entered.

Exactly, expressions with side-effects are error-prone and IMHO ugly. I
avoid them whenever I can. See also

http://en.wikipedia.org/wiki/Command-Query_Separation


August

Andrew Poelstra

unread,
Sep 4, 2008, 10:09:08 AM9/4/08
to
On Thu, 2008-09-04 at 16:01 +0200, August Karlstrom wrote:
> Sjoerd wrote:
> > Actually, some people don't use an explicit comparison and on top of that
> > use a non-trivial expression, such as this:
> >
> > while (line = fgets(buf, 10, fp)) ...
> >
> > This does not compare line to the result of fgets() because there is only
> > one = sign. Instead, this first assigns the result of fgets() to line and
> > then compares it to 0 to see if the loop should be entered.
>
> Exactly, expressions with side-effects are error-prone and IMHO ugly. I
> avoid them whenever I can. See also
>

I disagree.

while(line = fgets(buf, sizeof buf, fp))

is much clearer than the alternative

line = fgets(buf, sizeof buf, fp);
while(line != NULL) /* What does line mean? */
{
...
line = fgets(buf, sizeof buf, fp);
}

Fred

unread,
Sep 4, 2008, 10:31:06 AM9/4/08
to
On Sep 4, 7:09 am, Andrew Poelstra <apoels...@wpsoftware.net> wrote:
> On Thu, 2008-09-04 at 16:01 +0200, August Karlstrom wrote:
> > Sjoerd wrote:
> > > Actually, some people don't use an explicit comparison and on top of that
> > > use a non-trivial expression, such as this:
>
> > > while (line = fgets(buf, 10, fp)) ...
>
> > > This does not compare line to the result of fgets() because there is only
> > > one = sign. Instead, this first assigns the result of fgets() to line and
> > > then compares it to 0 to see if the loop should be entered.
>
> > Exactly, expressions with side-effects are error-prone and IMHO ugly. I
> > avoid them whenever I can. See also
>
> I disagree.
>
> while(line = fgets(buf, sizeof buf, fp))
>
> is much clearer than the alternative

I disagree - it is not at all clear what the original
author really intended. Did she mean to set line to the
result of fgets, or did she mean to compare line
to the result of fgets (and made a mistake)?

A much clearer way is to explicitly demonstrate
what was intended:

while( (line = fgets(buf, sizeof buf, fp)) != NULL )

--
Fred Kleinschmidt

fjb...@yahoo.com

unread,
Sep 4, 2008, 11:04:37 AM9/4/08
to

Indeed. Some compilers will warn about `while(line = fgets(...))' on
the grounds that you may have meant `while(line == fgets(...))'.
Adding the explicit test, as in `while((line = fgets(...)) != NULL)',
suppresses this warning.

Richard Tobin

unread,
Sep 4, 2008, 11:42:28 AM9/4/08
to
In article <23ac30b4-fe27-409c...@z11g2000prl.googlegroups.com>,
Fred <fred.l.kl...@boeing.com> wrote:

>> while(line = fgets(buf, sizeof buf, fp))

>I disagree - it is not at all clear what the original


>author really intended. Did she mean to set line to the
>result of fgets, or did she mean to compare line
>to the result of fgets (and made a mistake)?
>
>A much clearer way is to explicitly demonstrate
>what was intended:
>
>while( (line = fgets(buf, sizeof buf, fp)) != NULL )

Gcc - quite reasonably I think - shuts up provided you put parentheses
around the expression:

while( (line = fgets(buf, sizeof buf, fp)) )

I don't see any advantage to making the test against NULL explicit.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

Richard

unread,
Sep 5, 2008, 7:08:27 AM9/5/08
to
August Karlstrom <fusio...@gmail.com> writes:

The line above *is* C. If you think that is error prone I suggest you
consider another programming language.

August Karlstrom

unread,
Sep 5, 2008, 7:22:15 AM9/5/08
to

Command Query Separation is a general principle applicable to imperative
languages, C for instance. Nothing, except for the libraries I use,
stops me from using this principle in my programs.


August

0 new messages