473,480 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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");
}
}
Sep 4 '08 #1
11 2700
nembo kid wrote:
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
Sep 4 '08 #2
nembo kid <nembo@kidwrote:
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
Sep 4 '08 #3
nembo kid wrote:
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
Sep 4 '08 #4
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.
Sep 4 '08 #5
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
Sep 4 '08 #6
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 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);
}

Sep 4 '08 #7
On Sep 4, 7:09*am, Andrew Poelstra <apoels...@wpsoftware.netwrote:
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
Sep 4 '08 #8
On Sep 4, 7:31 am, Fred <fred.l.kleinschm...@boeing.comwrote:
On Sep 4, 7:09 am, Andrew Poelstra <apoels...@wpsoftware.netwrote:
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.
Sep 4 '08 #9
In article <23**********************************@z11g2000prl. googlegroups.com>,
Fred <fr*****************@boeing.comwrote:
>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.
Sep 4 '08 #10
August Karlstrom <fu********@gmail.comwrites:
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
The line above *is* C. If you think that is error prone I suggest you
consider another programming language.
Sep 5 '08 #11
Richard wrote:
August Karlstrom <fu********@gmail.comwrites:
>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

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
Sep 5 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2659
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
6
71924
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
1960
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
5
12219
by: é›· | last post by:
suggest add do while loop in later version
2
4686
by: Amanda | last post by:
If I do this, i get complaints saying that || cannot be used for char and bool. Any help? // options will be a,b,c,or e do { Console.Write("Enter a selection from the following list:\n" +...
10
2291
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
4
1745
by: nick048 | last post by:
Hi to all, I have founded a problem in my program; it is in this code: i=0; charRecv = recv(nSocketDesc, &c, 1, 0); while (c !='\n') { mystring=c; charRecv = recv(nSocketDesc, &c, 1, 0);
13
3760
by: icarus | last post by:
Hi all, i'm new to python. Learning on my own how to ask a user to finish a loop or not. For some reason, it behaves as infinite loop although I changed its condition. Please tell me what I'm...
4
1439
by: skanemupp | last post by:
it seems to me from my results that when i use a while-loop it will execute once after the condition is met. ie the conditions is met the code executes one time more and then quits.
2
1569
by: mingke | last post by:
If for example I have four arrays (with different value), and I have another array which is the result of calculation between these four arrays. Then, I can sort the final array to ascending order...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.