post  How to make something to loop forever.

kikolani (18)   Link to this post
If you want to make something to loop forever use

either

1
2
3
for(;;){
//put the code you want to loop forever here.
}


or

1
2
3
4
5
6
7
int temp = 0;
while (temp !=1){
/*
put the code you want to loop forever here.
Make sure you never put temp = 1 in the code you put.
*/
}
seymore15074 (449)   Link to this post
The first thing that comes to mind is when would anyone ever want to loop forever?

Anyway:
1
2
3
while( 1 )
{
}
Bazzy (4120)   Link to this post
I use infinite loops quite often so that I can test whether to stop the loop after certain actions
Zhuge (634)   Link to this post
Just curious, but am I the only one who uses while(true) {}? I don't think I've ever seen anyone use that; usually the empty for loop.

kikolani (18)   Link to this post
I usually use my "for" example if I need to make a console that does an action with numbers and I need that to loop forever in order for the console to ask you for different nembers every time so it performs the actions again with the different numbers. You can close the console after you have performed the actions with all of the numbers you need.
I can post the code for my console.
Last edited on
jmb272 (1)   Link to this post
I usually do this:

1
2
3
while (1 < 2) {
//code here.
}
jsmith (3807)   Link to this post
while( true ) or while( 1 ) IHMO is the best and most explicit.
seymore15074 (449)   Link to this post
I prefer while( 1 ) because it is usually familiar to Linux users, too.

$ while 1
> clear
> ls -ltr
> sleep 2
> end

To have a window in the background monitoring updates to a log or such file. Although while true or while 1 < 2 or others also work, I have heard them referred to by other professionals as a "while one".
kikolani (18)   Link to this post
I personally prefer while 1< 2.
Grey Wolf (1617)   Link to this post
1
2
3
4
5
6
7
#define FOREVER while(true)
...

FOREVER 
{

}
chris (73)   Link to this post
I believe while(true) is the best, as far as I am aware, while(true) & while(1) will be pretty much the exact same, however if you use something like while(1<2) then I think each time the loop iterates it has to check the conditional factor, for while(true) & while(1) there is nothing to check, but for 1<2 it has to check that, so a bit more overhead is used.
Duoas (3510)   Link to this post
Encouraged
Any one of the following

while (1)

while (true)

for (;;)

are both good and explicit. They are all instantly recognizable by C and C++ programmers as infinite loops, and the compiler explicitly understands them as such.


Discouraged
Any good compiler should also recognize

while (1 < 2)

to be identical to the first or second above, since "1 < 2" is a constant true expression. However, it is a bit odd to the eyes of nearly all C and C++ programmers, and should be (IMO) avoided.

Using the GCC on WinXP/Pentium, all the above loops compiled to a single JMP instruction.


Flagged variations
However, a loop like:
1
2
3
4
5
bool done = false;
while (!done)
  {
  ...
  }
does require code to check for continuation.


The power of and caveat to using (semi) infinite loops
Infinite loops are a wonderful control structure, because they give you goto powers without encumbering any ire from others, via the break and continue statements.

One form of loop not mentioned is the run-once:
1
2
3
4
5
do
  {
  if (foo) break;
  }
while (false);
It is more or less an explicit example of the goto workaround, and will be recognized by some as such.

It is often useful as a poor-man's (use in C only!) version of C++'s try..catch block.


Propriety
In all cases, however, I would suggest that the use of an infinite loop is likely an indication of design flaw. The number of times that it is "correct" to use one is far fewer than given in practice...

My $0.02.

Hope this helps.
Last edited on
Mythios (888)   Link to this post
Alright so Duoas I follow everything you said. Just one question. Lets say in a real world situation I used for(;;) to make an infinite loop in my program. Would my employer want to throw stones at me?
Duoas (3510)   Link to this post
It depends on your employer.

(Each employer will have coding guidelines you should follow.)

;-)
Last edited on
Mythios (888)   Link to this post
Haha, alright thanks mate :D
jdd (95)   Link to this post
Infinite loops may not be considered good practice when programming for computer platforms. However, they are absolutely essential in embedded systems. If a microprocessor's program is allowed to finish execution, the microprocessor goes dead and the associated hardware becomes useless. To prevent this from happening, we put most or all of the code inside an infinite loop, ensuring that the system stays active until it is meant to be turned off.

Just thought I'd throw out this example of the usefulness of infinite loops.
Mythios (888)   Link to this post
Yeah - that's one thing I do know about infinite loops. Makes them really handy in such cases as you said.
David C Black (1)   Link to this post
Infinite loops are also good for hardware simulation (e.g. see systemc.org for a C++ based standard that does this) if you have independent threads. Each thread simulates a piece of hardware (assume power stays on). Hardware doesn't finish (unless destroyed).

Infinite loops are also good in threads that support windows. Inside the loop, they watch for events and process. This is common in GUI's.

One thought on which coding to use: for(;;) has the same number of characters as forever... nice mnemonic device to remember it by.
Mythios (888)   Link to this post
@ David C Black
One thought on which coding to use: for(;;) has the same number of characters as forever... nice mnemonic device to remember it by.


Lol :D
adrianvidal (16)   Link to this post
is this article for crashing computers?

Pages: [1] [2]


This topic is archived - New replies not allowed.