answersLogoWhite

0

How do you fix an infinite loop?

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

It comes from its name: it doesn't terminate, the user have to interrupt the program-run (in the worst case: power off the computer).

The infinite loop is also used to program loops with non-easily-deterministically end-of-loop conditions.

You write an infinite loop, such as for (;;) {statements}, and break out of the loop with the break statement when ready to terminate.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

You stop an infinite loop with the break statement.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You must make a terminating condition, something that will cause your loop to exit. For example, loop while count < 10 and increment count in every iteration of your loop.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you fix an infinite loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A program with a loop that never ends is called an?

Infinite loop.


What is counter loop?

A counted loop is a loop that executes the loop's statement a pre-determined number of times. The count represent the exit condition of the loop. A loop that is not counted is an infinite loop.


What is the shortcut key in laptop to stop infinite loop of turbo c?

567


How do you break the infinite loop program in turbo c plus plus without break key?

In short, you don't. By definition, an infinite loop is one that has no exit condition. For example: while( 1 ); When execution reaches this line, the program will get no further. It will continually execute this same line of code because the only way for this loop to exit is when the literal constant 1 becomes the literal constant 0 -- which would clearly be impossible. By the same token, the following lines are also infinite loops: do while( 1 ); for(;;); Infinite loops of this kind are completely useless in production code. Apart from the fact they do nothing remotely useful, an infinite loop renders the program completely invalid. And yet we hear the term infinite loop being used in code all the time. The truth is that while we do use the term infinite loop, it is not strictly infinite. What we really mean is that although the loop statement needn't have an exit condition, the body of the loop does! By way of an example, consider the following infinite loop: while( 1 ) { if( !rand() ) break; } Here, the loop statement is clearly infinite, but the body contains the all-important exit condition. In this case, the loop continually produces random numbers in the range 0 to 32767. If the number is zero, then control passes to the break statement at which point the loop ends and execution continues at the statement following the closing brace of the loop. Thus on every loop there's a 1 in 32768 chance the loop will end. There's a remote possibility the loop may never end however it's highly unlikely. Note that in its current form, the random numbers will produce the same sequence over an over and the loop will therefore execute the same number of times every time the program is run. But even if we seed the random numbers to produce a different sequence each time, the loop will exit after just a few seconds at most. The odds of never ever producing a zero are remote in the extreme. So that's the so-called infinite loop -- which is not really infinite after all. But to answer the question, we break out of an infinite loop by providing at least one exit condition within the body of the loop. If the program is to be run within an event-driven environment (such as Windows), we might periodically test the message queue to see if a specific key sequence is pending (such as ESC) and prompt the user to save the current state of the loop so that it can continue at a later date before gracefully exiting the loop. By the same token, we should also check for a program exit message (perhaps the user clicked the close window button, or even chose to shutdown Windows in the middle of the loop) and we must cater for those eventualities too. Ultimately, how you exit an infinite loop will depend entirely upon the purpose of the loop. There is no one-size-fits-all solution. But there must be at least one exit condition that must logically fire at some point in time. If not, the loop is truly infinite and will completely invalidate your program.


Why it is necessary to avoid infinite loop in program design?

It is not necessary to avoid infinite loops. You are perhaps confusing infinite loops with endless loops which are to be avoided at all costs. An endless loop is an infinite loop that has no reachable exit condition; the loop will iterate until we forcibly terminate the program. We use the the term infinite loop in the sense that it is impossible to measure or calculate when the exit point will be hit. the following are all examples of infinite loops in their simplest form: for (;;) { // ... } while (true) { // ... } do while (true) { // ... } endless: // ... goto endless; The conditional expressions in each of these loops can never be false thus we cannot easily determine when these loops will exit. We typically use infinite loops when there are many exit conditions to consider and it is either impractical or inefficient to evaluate all of those conditions via the controlling expression alone. We take it as read the exit conditions are contained within the body of the loop. If the body of the loop has no reachable exit condition then it becomes an endless loop. It is the programmer's responsibility to ensure that all infinite loops can exit at some point.

Related questions

How do you write a infinite for loop?

for(; ;);..this will do for an infinite loop


A program with a loop that never ends is called an?

Infinite loop.


Differences between finite and infinite loop?

A function is set of instruction that perform some specific operation . There are two type of loops one is finite and other is infinite. A finite loop is one that end after finite runs and an infinite loop is one that never ends itself.


How do you loop a program in python?

An infinite loop might look something like: while 1==1: print("Infinite loop") as 1 is ALWAYS equal to 1.


What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


When a VBScript loop control variable is not altered during loop execution what kind of loop may result?

An infinite loop.


What are the release dates for Infinite Loop - 2012 II?

Infinite Loop - 2012 II was released on: USA: 24 November 2012 (internet)


The Loop it's best to be out of?

An infinite loop - one that never stops. Unless that is what you intended.


What is a Python loop?

A Python loop is something that will always happen or continue to happen until the condition isn't met. So for example:while 1==1:print("Infinite loop")would be an infinite loop, as 1 will ALWAYS be equal to 1.


What is the address for apple?

1 Infinite LoopCupertino, California 95014United States of America


Where is apple headquarters at?

Apple Inc.1 Infinite Loop Cupertino, CA 95014


How do you do a program looping?

An infinite loop might look something like: while 1==1: print("Infinite loop") as 1 is ALWAYS equal to 1.