473,659 Members | 2,632 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
52 21912
Noah Roberts wrote:
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


I didn't think this was valid syntax (after macro substitution):
do
{
/* ...x... */
} for (;;); /* after preprocessor substitution */

My understanding is the syntax is:
do
{
} while();

I don't believe you can replace the "while" with "for" in this
syntax. I've never heard of a "do-for" loop.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #11
"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? If so, should we then use for(;;)? Thanks,


for(;;) has seven characters, and
while(1) has eight, so the former should
weight slightly less, thus be a bit more
portable.

Also, if carrying the construct in a backback,
the rounded edges of 'f' 'o' and 'r' should
be more comfortable than those pointy 'w', 'h',
'i', and 'l' characters. :-)

Seriously, either one is equally portable. Use
whichever you find most natural, unless you have
coding standards to follow.

-Mike
Nov 13 '05 #12
Rick wrote:
Jeremy Yallop writes:

[snip]

I didn't write any of the quoted text. Please be more careful with
your attributions.

Jeremy.
Nov 13 '05 #13
pete wrote:

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


It's just your compiler's rant about programming style. The while (1)
and for (;;) constructs are identical.
--
Joe Wright mailto:jo****** **@earthlink.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #14
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? If so, should we then use for(;;)? Thanks,

Rick


Well I use

while ( 1 )

if I wish to.

BUT

I use

for ( ; ; )

If I wish to.

In terms of performance any *intelligent* compiler would produce the
same binary code. Though a compiler made possibly as a *experimental
project* **might** not do the optimization and might include a sort of
condition for checking the truth value of the expression in while,
which would be true, always. OTOH if you love goto you might also
wanna use

inf_loop:
......
goto inf_loop;

The last one would have the same performence level as that of a for( ;
; ).

why not use

for ( ; 1 ; )

which is *completly* semantically equivalent to the while ( 1 )

--
Imanpreet Singh Arora
imanpreet_arora AT yahoo DOT co DOT in

Aut inveniam viam aut faciam
Nov 13 '05 #15
Sorry about that Jeremy, I meant to qoute Osmium.
Rick

Jeremy Yallop wrote:
Rick wrote:
Jeremy Yallop writes:


[snip]

I didn't write any of the quoted text. Please be more careful with
your attributions.

Jeremy.


Nov 13 '05 #16
Rick writes:
Sorry about that Jeremy, I meant to qoute Osmium.
Rick

Jeremy Yallop wrote:
Rick wrote:
Jeremy Yallop writes:


[snip]

I didn't write any of the quoted text. Please be more careful with
your attributions.


I didn't write it either. Try again.
--
Osmium
Nov 13 '05 #17
Thomas Matthews wrote:
Noah Roberts wrote:
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


I didn't think this was valid syntax (after macro substitution):


That was, I believe, the point he was making (i.e. that for(;;) and while(1)
are not interchangeable in all cases).

For what it's worth, I think while(condition ) is clearer than either for(;;)
or while(1).

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #18

On Tue, 30 Sep 2003, Ridimz wrote:

"osmium" <r1********@com cast.net> wrote in message

I didn't write it either. Try again.
--
Osmium


Yea you did! Read your reply again! It's attached just in case
you need to see which one!


Maybe osmium means that someone else has been posting to Usenet
using his account... ;-)

Please don't post attachments to non-binaries groups.
A simple link to Google Groups, or, even better, a quick
reminder to "look three messages up the darn thread and
*read* it" would have sufficed.

And in general, don't use spaces in filenames. Some
uudecode clients might not even parse your attachment
correctly (I dunno what any RFC on the topic says; I'm
just pointing out the possibility).

-Arthur

Nov 13 '05 #19
Thomas Matthews <Th************ **********@sbcg lobal.net> wrote in message news:<AO******* **********@news svr17.news.prod igy.com>...
Noah Roberts wrote:
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


I didn't think this was valid syntax (after macro substitution):
do
{
/* ...x... */
} for (;;); /* after preprocessor substitution */

My understanding is the syntax is:
do
{
} while();


Nitpick: replace while() with while(expr)
I don't believe you can replace the "while" with "for" in this
syntax. I've never heard of a "do-for" loop.


Me neither. A lot of books would need to be appended if it existed.
--
Imanpreet Singh Arora
iimmaannpprreee ett_aarroorraa@ @y.....cc oo..iinn

Singularize the letters above to send mail.
Nov 13 '05 #20

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
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
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
1982
muto222
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.