|  | while( ) and test condition |  | |
| | | nembo kid |  |
| Posted: Thu Sep 04, 2008 8:11 am Post subject: 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"); }
} |
| |
| | | pete |  |
| Posted: Thu Sep 04, 2008 8:14 am Post subject: 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 |
| |
| | | Richard Bos |  |
| Posted: Thu Sep 04, 2008 8:16 am Post subject: Re: while( ) and test condition |  |
nembo kid <nembo@kid> wrote:
| 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 |
| |
| | | August Karlstrom |  |
| Posted: Thu Sep 04, 2008 8:30 am Post subject: 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 |
| |
| | | Sjoerd |  |
| Posted: Thu Sep 04, 2008 11:45 am Post subject: 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. |
| |
| | | August Karlstrom |  |
| Posted: Thu Sep 04, 2008 12:01 pm Post subject: 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
LINK
August |
| |
| | | Andrew Poelstra |  |
| Posted: Thu Sep 04, 2008 12:09 pm Post subject: Re: while( ) and test condition |  |
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:
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); } |
| |
| | | Richard Tobin |  |
| Posted: Thu Sep 04, 2008 1:42 pm Post subject: Re: while( ) and test condition |  |
In article <23ac30b4-fe27-409c-9c41-6542d5498421@z11g2000prl.googlegroups.com>, Fred <fred.l.kleinschmidt@boeing.com> wrote:
| Quote: | 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. |
| |
| | | Fred |  |
| Posted: Thu Sep 04, 2008 2:31 pm Post subject: Re: while( ) and test condition |  |
| |  | |
On Sep 4, 7:09 am, Andrew Poelstra <apoels...@wpsoftware.net> wrote:
| Quote: | 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 |
| |
| | | Guest |  |
| Posted: Thu Sep 04, 2008 3:04 pm Post subject: Re: while( ) and test condition |  |
| |  | |
On Sep 4, 7:31 am, Fred <fred.l.kleinschm...@boeing.com> wrote:
| Quote: | 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 )
|
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. |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|