473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

infinite loops

Is any infinite loop better than other? Is there any difference between
there efficiency?

Mar 26 '06 #1
13 2257
Vector wrote:
Is any infinite loop better than other? Is there any difference between
there efficiency?


To what use will you put the answer? A compiler will treat for(;;) and
while(true) the same. The second takes longer to type. Always optimize
programming time.

The second might trigger a "conditional expression is constant" warning of
some type.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 26 '06 #2
Vector wrote:
Is any infinite loop better than other? Is there any difference between
there efficiency?

Define better.

Which conveys the intent better? I'd say while( true ){}

Efficiency shouldn't be an issue.

--
Ian Collins.
Mar 26 '06 #3
Vector wrote:
Is any infinite loop better than other? Is there any difference between
there efficiency?


I'm going to assume this is a joke.

Have you determined that the infinite loop is your bottleneck? How
long does your infinite loop take to complete? Have you benchmarked to
see if your infinite loop runs in aleph-null time versus aleph-one time?

Mar 26 '06 #4
Vector posted:
Is any infinite loop better than other? Is there any difference between
there efficiency?

In practise, they are the same. Compilers will produce the exact same
machine code for these:

for (;;) { int k = 7; }

while (true) { int k = 7; }

do { int k = 7; } while (true);
In theory, the first one is fastest because there's no condition being
tested for each iteration of the loop.

I myself use for(;;).
-Tomás
Mar 26 '06 #5
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}

Mar 26 '06 #6

Gaijinco wrote:
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}

well answer is very obvious
it will print negative number that is shortest one.
reason : consider the same code with little modification for ur
understanding
int main()
{
char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
i increments from 1 to 127 than incrementing further will make it 127
to -128
this is wat the range of char -128 to 127.

Mar 26 '06 #7
Gaijinco posted:
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}

As far as I know it's undefined behaviour for a signed integral type to go
past its limit.

In practise though, on a 32-Bit system, I'd hazard a guess that it'd be one
of the following:

0
-1
-4294967296
-Tomás
Mar 26 '06 #8
am*********@gmail.com wrote:

Gaijinco wrote:
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}

well answer is very obvious
it will print negative number that is shortest one.
reason : consider the same code with little modification for ur
understanding
int main()
{
char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
i increments from 1 to 127 than incrementing further will make it 127
to -128


It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.
Mar 26 '06 #9
On 25 Mar 2006 20:42:34 -0800, "Vector" <su***********@gmail.com>
wrote in comp.lang.c++:
Is any infinite loop better than other? Is there any difference between
there efficiency?


How could one infinite loop be better than another? Could one be
infinitely more infinite than the other? If one ends before the other
does, the comparison was invalid because the one that ends was not an
infinite loop to begin with.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 27 '06 #10
Jack Klein wrote:
How could one infinite loop be better than another? Could one be
infinitely more infinite than the other? If one ends before the other
does, the comparison was invalid because the one that ends was not an
infinite loop to begin with.


Haw haw.

That sophistry is either a play on James Kanze's latest post to the thread
"Infinite loop == undefined behaviour?"...

http://groups.google.com/group/comp....69f9d5e793835b

....or a supernatural channeling of it, if you hadn't read it yet.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 27 '06 #11
Rolf Magnus wrote:
am*********@gmail.com wrote:

char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128


It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.


This is actually a case of out-of-range assignment: i is promoted
to int for the expression ++i, and the result is then reassigned
to i.

Mar 27 '06 #12
Old Wolf wrote:
Rolf Magnus wrote:
am*********@gmail.com wrote:

char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.


This is actually a case of out-of-range assignment:


<nitpick>unless char is signed and sizeof(int)==sizeof(char)</nitpick>.
i is promoted to int for the expression ++i, and the result is then
reassigned to i.


The other example that used int for i was doing what I wrote. Anyway, what
would be the behavior here? Is the value just truncated (modulo) or is it
UB too?

Mar 27 '06 #13
Rolf Magnus wrote:
Old Wolf wrote:
am*********@gmail.com wrote:

char i=1;

while(i>0)
++i; This is actually a case of out-of-range assignment:


<nitpick>unless char is signed and sizeof(int)==sizeof(char)</nitpick>.


Of course :)
The other example that used int for i was doing what I wrote. Anyway, what
would be the behavior here? Is the value just truncated (modulo) or is it
UB too?


Being signed, I think it is a case of implementation-defined
behaviour or an i-d signal in C99. (

Mar 27 '06 #14

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
8
by: bob | last post by:
periodically, my program seems to get stuck in an infinite loop. i have a lot of loops though, so i don't know which one. any ideas how to find out? i'm using dev-cpp. i think having the...
7
by: Andrej Hocevar | last post by:
Hello, what is the best way of solving cases where one has to wait (forever, until killed or instructed otherwise) for some event to occur? E.g., wait for data, monitoring a file? In fact, that...
2
by: psg | last post by:
Hello! I am developing Windows application which after sending a request to a server will listen to response, a response will be infinite in length, but will not be continuous in time. What I...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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,...
0
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...
0
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...
0
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,...

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.