Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 11:15 AM
nembo kid
Guest
 
Posts: n/a
Default while( ) and test condition

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");
}


}
  #2  
Old September 4th, 2008, 11:15 AM
pete
Guest
 
Posts: n/a
Default Re: while( ) and test condition

nembo kid wrote:
Quote:
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");
}
>
>
}
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
  #3  
Old September 4th, 2008, 11:25 AM
Richard Bos
Guest
 
Posts: n/a
Default Re: while( ) and test condition

nembo kid <nembo@kidwrote:
Quote:
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
  #4  
Old September 4th, 2008, 11:35 AM
August Karlstrom
Guest
 
Posts: n/a
Default Re: while( ) and test condition

nembo kid wrote:
Quote:
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");
}
>
>
}
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
  #5  
Old September 4th, 2008, 02:55 PM
Sjoerd
Guest
 
Posts: n/a
Default Re: while( ) and test condition

On Thu, 04 Sep 2008 12:30:29 +0200, August Karlstrom wrote:
Quote:
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.
  #6  
Old September 4th, 2008, 03:05 PM
August Karlstrom
Guest
 
Posts: n/a
Default Re: while( ) and test condition

Sjoerd wrote:
Quote:
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
  #7  
Old September 4th, 2008, 03:15 PM
Andrew Poelstra
Guest
 
Posts: n/a
Default Re: while( ) and test condition

On Thu, 2008-09-04 at 16:01 +0200, August Karlstrom wrote:
Quote:
Sjoerd wrote:
Quote:
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 lineand
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);
}

  #8  
Old September 4th, 2008, 03:35 PM
Fred
Guest
 
Posts: n/a
Default Re: while( ) and test condition

On Sep 4, 7:09*am, Andrew Poelstra <apoels...@wpsoftware.netwrote:
Quote:
On Thu, 2008-09-04 at 16:01 +0200, August Karlstrom wrote:
Quote:
Sjoerd wrote:
Quote:
Actually, some people don't use an explicit comparison and on top of that
use a non-trivial expression, such as this:
>
Quote:
Quote:
while (line = fgets(buf, 10, fp)) ...
>
Quote:
Quote:
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.
>
Quote:
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
  #9  
Old September 4th, 2008, 04:05 PM
fjblurt@yahoo.com
Guest
 
Posts: n/a
Default Re: while( ) and test condition

On Sep 4, 7:31 am, Fred <fred.l.kleinschm...@boeing.comwrote:
Quote:
On Sep 4, 7:09 am, Andrew Poelstra <apoels...@wpsoftware.netwrote:
>
>
>
Quote:
On Thu, 2008-09-04 at 16:01 +0200, August Karlstrom wrote:
Quote:
Sjoerd wrote:
Actually, some people don't use an explicit comparison and on top of that
use a non-trivial expression, such as this:
>
Quote:
Quote:
while (line = fgets(buf, 10, fp)) ...
>
Quote:
Quote:
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.
>
Quote:
Quote:
Exactly, expressions with side-effects are error-prone and IMHO ugly. I
avoid them whenever I can. See also
>
Quote:
I disagree.
>
Quote:
while(line = fgets(buf, sizeof buf, fp))
>
Quote:
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 )
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.
  #10  
Old September 4th, 2008, 04:45 PM
Richard Tobin
Guest
 
Posts: n/a
Default Re: while( ) and test condition

In article <23ac30b4-fe27-409c-9c41-6542d5498421@z11g2000prl.googlegroups.com>,
Fred <fred.l.kleinschmidt@boeing.comwrote:
Quote:
Quote:
>while(line = fgets(buf, sizeof buf, fp))
Quote:
>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.
  #11  
Old September 5th, 2008, 12:15 PM
Richard
Guest
 
Posts: n/a
Default Re: while( ) and test condition

August Karlstrom <fusionfile@gmail.comwrites:
Quote:
Sjoerd wrote:
Quote:
>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
The line above *is* C. If you think that is error prone I suggest you
consider another programming language.
  #12  
Old September 5th, 2008, 12:25 PM
August Karlstrom
Guest
 
Posts: n/a
Default Re: while( ) and test condition

Richard wrote:
Quote:
August Karlstrom <fusionfile@gmail.comwrites:
>
Quote:
>Sjoerd wrote:
Quote:
>>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
>
The line above *is* C. If you think that is error prone I suggest you
consider another programming language.
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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles