Is any infinite loop better than other? Is there any difference between
there efficiency? 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!!!
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.
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?
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
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;
}
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.
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 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.
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!!!
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.
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?
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. ( This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
by: rami |
last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
|
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....
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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...
|
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,...
| |