473,902 Members | 3,721 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 22008
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 #21
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<PZ******* **********@news read4.news.pas. earthlink.net>. ..
"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.


Mine for loop

for ( ; ; )

has 11 characters

so does my while

while ( 1 )

including whitespace I might add.

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. :-)
I guess it all boils down to this *VERY* difference only.

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


--
Imanpreet Singh Arora
imanpreet_arora AT yahoo DOT co DOT in
Nov 13 '05 #22
Richard Heathfield wrote:
Thomas Matthews wrote:

Noah Roberts wrote:
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).


Yeah, that was the point I was making. Some people like to create a
macro like that, I usually just use the actual loop when I am testing
code. I don't like to use forever loops in real code but if I where to
do so I might define something like that.
For what it's worth, I think while(condition ) is clearer than either for(;;)
or while(1).


Yes, it is much better to use a condition to exit. Many times people do
something like the following:

while (always)
get input
if input is quit then die

do something
....

but it is much better do do something like this:

boolean quit = false /* or continue = true */

while (!quit)
get input
if input is quit then quit = true
else do something
....

The second version is "structured " code whereas the first is not (two
exits). Sometimes it is better to break structure, but it always
sacrifices maintainability and readability; this is why goto was
declaired to be evil, yet in some algorithms (for instance verified null
move pruning in chess AI's) it is useful.

99.9999999% of the time it is not necissary to write unstructured code
and since it is a mistake to do so you shouldn't except for that
..000000001% of the time when you need to.

NR

Nov 13 '05 #23
Joe Wright wrote:

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.

It's just your compiler's rant about programming style. The while (1)
and for (;;) constructs are identical.


That's why I let the rant be the tie breaker.

--
pete
Nov 13 '05 #24
"Minti" <mi************ @yahoo.com> wrote in message
news:e8******** *************** ***@posting.goo gle.com...
"Mike Wahler" <mk******@mkwah ler.net> wrote in message

news:<PZ******* **********@news read4.news.pas. earthlink.net>. ..
"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.


Mine for loop

for ( ; ; )

has 11 characters

so does my while

while ( 1 )

including whitespace I might add.

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. :-)


I guess it all boils down to this *VERY* difference only.


Everyone knows that whitespace characters are weightless. :-)

-Mike
Nov 13 '05 #25
Noah Roberts writes:
but it is much better do do something like this:

boolean quit = false /* or continue = true */


I wouldn't expect the alternative to work.
Pedants, especially, should test their code before positng.
Nov 13 '05 #26
osmium wrote:
Noah Roberts writes:

but it is much better do do something like this:

boolean quit = false /* or continue = true */

I wouldn't expect the alternative to work.
Pedants, especially, should test their code before positng.


If you use continue = true you must alter the loop just slightly.

while (continue)
{

if (wanttoquit) continue = false;
else dostuff...
}

I guess some people must have it spelled out for them, I assumed too
much. And no, I was not being pedantic but you have the right to that
opinion.

NR

Nov 13 '05 #27
Noah Roberts <nr******@donte mailme.com> wrote:
while (continue)


Erm... choose a different name.

Richard
Nov 13 '05 #28
In article <bl**********@q uark.scn.rain.c om>, Noah Roberts wrote:
osmium wrote:
Noah Roberts writes:

but it is much better do do something like this:

boolean quit = false /* or continue = true */

I wouldn't expect the alternative to work.
Pedants, especially, should test their code before positng.


If you use continue = true you must alter the loop just slightly.

while (continue)
{

if (wanttoquit) continue = false;
else dostuff...
}

I guess some people must have it spelled out for them, I
assumed too much. And no, I was not being pedantic but you
have the right to that opinion.


continue is a keyword.

--
Neil Cerutti
Nov 13 '05 #29
Noah Roberts wrote:
Yes, it is much better to use a condition to exit. Many times people do
something like the following:

while (always)
get input
if input is quit then die

do something
...

but it is much better do do something like this:

boolean quit = false /* or continue = true */
I'm sure that, being a well-structuring kind of chap, you just forgot
that `continue` is a C keyword ...
while (!quit)
get input
if input is quit then quit = true
else do something
...

The second version is "structured " code whereas the first is not (two
exits).
The second one is horrid. The artifical boolean variable obscures what's
going on.

Assuming `always` is well-named, the first one has on exit. N-and-a-half
loops are just as structured as N loops. The idiom

while (yourChoiceForT rue)
{ preamble;
if (doneExpression ) break;
postamble; }

is well-structured and worth knowing.
Sometimes it is better to break structure, but it always
sacrifices maintainability and readability;


Not *always*.

The rule that makes sense to me is "code clearly". If it's a choice
between between being clear and being structured, being clear wins.
Usually you don't need to make the choice.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #30

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

Similar topics

24
2725
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
2823
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
2336
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
71995
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
1995
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
3203
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
12256
by: é›· | last post by:
suggest add do while loop in later version
3
1859
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
2100
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
2741
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
9997
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
11279
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
10981
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
10499
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
9673
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...
0
5893
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4725
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
3
3323
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.