473,660 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for(;;) or while(1)?

Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,

Rick

Nov 13 '05 #1
52 21913
In article <3f********@cla rion.carno.net. au>, Rick wrote:
Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,


Some C compilers may generate slightly different machine code,
but they ought to always be functionally identical. Choosing
one instead of the other will not break a program or make it
unportable.
--
Andreas Kähäri
Nov 13 '05 #2
j

"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C?
No.
If so, should we then use for(;;)? Thanks,

You use for(;;) when you do not want to imply a test, otherwise for that
purpose, you use while(1)
The practical result will be the same for all but the very stupidest
compilers.
Rick



Nov 13 '05 #3
Rick wrote:

Hi,

For portability, can an issue arise if we use while(1)
for an infinite loop in C?
My compiler generates a warning for that kind of code,
when I have the warning level high, where I like it.
If so, should we then use for(;;)?


My compiler doesn't generate a warning for that code,
so that's what I use.

--
pete
Nov 13 '05 #4
"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
| For portability, can an issue arise if we use while(1) for an infinite
| loop in C? If so, should we then use for(;;)? Thanks,

It should come down to the same.

However, I use for(;;) because I use some compilers which send out
warnings of the kind: "Constant used as conditional expression".

go figure...
--
http://ivan.vecerina.com
Nov 13 '05 #5
Rick wrote:
For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,


There aren't any real portability issues, but the `1' seems too
arbitrary to me (as pointed out in <news:sl******* *********@ashi. FootPrints.net> ,
which is where I first saw that objection, I think. Using `true'
seems less objectionable, somehow, if you have a C99 (or C++) compiler
(or define it yourself).

Jeremy.
Nov 13 '05 #6
Greetings.

In article <bl************ @ID-114079.news.uni-berlin.de>, Jeremy Yallop
wrote:
For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,


There aren't any real portability issues, but the `1' seems too
arbitrary to me


More so than while(42)? There's plenty of precedent for using '1' as a
generic truth constant; less so for other nonzero numbers.

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #7
Jeremy Yallop writes:
For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,
There aren't any real portability issues, but the `1' seems too
arbitrary to me (as pointed out in

<news:sl******* *********@ashi. FootPrints.net> , which is where I first saw that objection, I think. Using `true'
seems less objectionable, somehow, if you have a C99 (or C++) compiler
(or define it yourself).


I wouldn't expect *any* constant to get rid of the warning message. while
implies a condition that will vary. I can see the reason for the message
but I'm glad the compiler I have doesn't issue such a warning. I much
prefer the while(1) construct. The for(:::) looks to me like someone is
trying to impress someone with their arcane knowledge.
Nov 13 '05 #8
Rick wrote:
Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,

Rick


Well using it directly in code it has no matter, but what about this:

#define forever for(;;)

do
{
...x...
} forever;

NR

Nov 13 '05 #9
> Jeremy Yallop writes:
I wouldn't expect *any* constant to get rid of the warning message.
Wasn't that what Ivan said.. the compiler issues a "Constant used as
conditional expression" warning. How will *any* constant get rid of the
warning when the warning is about the use of a constant itself?
I much prefer the while(1) construct. The for(:::) looks to me like someone is
trying to impress someone with their arcane knowledge.


I don't think it's about making impressions, I'd say it's a matter of
taste. I myself use for(;;).. I believe it's much clearer to what's
happening; and like you said "while implies a condition that will vary",
that's just why while(constant) can be a little misleading at first
glance. My compiler once gave me a warning for while(constant) , I
wondered if using such a constract was non-portable, but that's clearly
not the case.

Rick

Nov 13 '05 #10

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

Similar topics

24
2697
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:
36
2795
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the beginning of while_1 }
5
2318
by: Tom Kent | last post by:
I'm running into a weird circumstance with Visual Studio 7.1, where it will not properly indent a while loop nested inside a do-while loop. It compiles and runs fine, but the automatic indenting messes it up every time. Here's an example: using System; namespace Do_While_Test {
6
71965
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
1972
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 a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the correct number, at which point you then tell the user "Correct". I tried using the While Loop, and...
7
3193
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
5
12230
by: é›· | last post by:
suggest add do while loop in later version
3
1842
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on the 2nd while loop (contained within the 1st), bcos the 2nd while loop changes the condition evaluated by the if statement which means another while loop (executed where if evaluates differently) gets executed straight afterwards
10
2071
by: wd.jonsson | last post by:
Hmm, my while loop with "or" doesn't seem to work as I want it to... How do I tell the while loop to only accept "Y" or "y" or "N" or "n" input from the str(raw_input)? Thank's in advance! Snippet of code: import os
11
2720
by: nembo kid | last post by:
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>
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2760
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 we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.