DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

In computer science, an endless loop (or infinite loop) refers to a block of code which will never cease execution, resulting in the program getting “stuck” in a certain instruction or set of instructions. This usually causes the program - or even the entire operating system - to become unresponsive, due to the large amount of instructions being executed.

Intentional Endless Loops

Creating an intentional endless loop can easily be done with a “while(True)” statement in code. In general, this is considered poor programming style in most high-level languages since they are generally avoidable and can be removed with some basic refactoring. However, there are a few practical reasons why a programmer might want to create an endless loop intentionally:

Waiting for an interrupt

An external interrupt - such as a particular key press or a signal from a different process - would most likely require the use of an infinite loop around the interrupt check in order to execute properly. One example is a mutex lock, used in parallel programming to ensure that multiple threads converge after they finish execution. Here is what the sample code for a mutex might look like:

while (true)
{
    if (workLeftToDo())
    {
        assignWork(getAvailableThread())
    }
    else
    {
        if (threadsNotBusy())
        {
            break;
        }
    }
}

When using a "break" statement is clearer

Just about all high level languages support “break” statements, which will force-exit from one level of looping. Using this instead of checking a flag or boolean variable can help simplify code in some cases. For example, consider the following:

while (true)
{
    doStuffNeededAtStartOfLoop();
    int input = getSomeInput();
    if (testCondition(input))
    {
        break;
    }
    actOnInput(input);
}

Using a break in this situation removes the need for an extra “else” statement. Note that if the language supported “do/while” loops, a do/while loop could also potentially eliminate the need for the else statement (but it would require the use of a flag variable).

Unintentional Endless Loops

In this case, the endless loop is a painful bug in the program that will cause unexpected behavior - namely, the program never exiting. The process will most likely have to be forcibly stopped and the issue debugged before normal execution can again take place.

There are many situations which could provoke an unintentional endless loop, including (but not limited to) the following:

Bad exit condition

If an exit condition is too specific (or is never reached) a loop will never cease executing, and result in an endless loop. Keeping exit conditions too general is itself a problem, but may be preferable to an endless loop situation. For example,

while(i != 5)
{
    i = i + getSomePositiveNum();
    printf("%d\n", i);
{

In this code, if the getSomePositiveNum function increments the variable i to any value above 5, the loop will not exit properly. For example, if i == 3 going into the loop and getSomePositiveNum() returns 3, then i == 6 after the increment and will never be reduced in value…so the loop will continue on forever!

A better check would be “while(i < 5)” as this would avoid the above scenario; if i == 6, it would still exit properly.

Infinite recursion

Infinite loops are not unique only to iterative functions; recursive functions can also succumb to this if the base case(s) in recursion are never achieved. For example,

int recur(int foo, int bar) {
    if(foo == 0) {
        return(1 + bar);
    }
    else {
        recur(foo, bar-1);
    }
}

Although the exit condition here depends on the variable “foo”, it remains unchanged when it is passed into the recursive function call. The integer “bar”, on the other hand, is changed. As such, this function will never hit the only return statement in its current form; in order to fix this, the exit condition will need to depend on “bar” intead, or the function call must be changed to recur(foo-1, bar).

Categories


QR Code
QR Code endless_loop (generated for current page)
 

Advertise with Anonymous Ads