|
| kikolani (18) | |||||
| If you want to make something to loop forever use either
or
| |||||
| seymore15074 (446) | |||
| The first thing that comes to mind is when would anyone ever want to loop forever? Anyway:
| |||
| Bazzy (3164) | |
| I use infinite loops quite often so that I can test whether to stop the loop after certain actions | |
| Zhuge (324) | |
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) | |
| 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) | |||
I usually do this:
| |||
| jsmith (3099) | |
| while( true ) or while( 1 ) IHMO is the best and most explicit. | |
| seymore15074 (446) | |
| 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) | |
| I personally prefer while 1< 2. | |
| Grey Wolf (1407) | |||
| |||
| chris (70) | |
| 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 (2964) | |||||
| 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:
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:
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 (753) | |
| 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 (2964) | |
| It depends on your employer. (Each employer will have coding guidelines you should follow.) ;-) | |
Last edited on | |
| Mythios (753) | |
| Haha, alright thanks mate :D | |
| jdd (95) | |
| 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 (753) | |
| 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) | |
| 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 (753) | ||
@ David C Black
Lol :D | ||
| adrianvidal (16) | |
| is this article for crashing computers? | |
Pages: [1] [2]
This topic is archived - New replies not allowed.
