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");
}
} 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
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
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
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.
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
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);
}
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
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.
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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?
|
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...
|
by: é›· |
last post by:
suggest add do while loop in later version
|
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" +...
| |
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;
|
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);
|
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...
|
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.
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |