5

I'm using a

while (true)
{
    if (x == y)
    {
        break;
    }
    else
    {
        //do stuff
    }
}

loop like so, the frame is just an example frame, as the actual code itself is convoluted and overly complicated that it requires a "break;" statement in multiple different areas for multiple different conditions. My question is; Is there a way to write a loop statement without the loop checking for a condition at all? Is there a more efficient way to write an infinite loop other than while(true)?

edit: (java)

edit2:

while (a < b)
{
    while (true)
    {
        if (c < d)
        {
            if (e == null)
            {
                //do alot of stuff
                break;
            }
            else
            {
                //do something
            }
        }
        else if (d > c)
        {
            if (e == null)
            {
                //do alot of stuff
                break;
            }
            else
            {
                //do something
            }
        }
        else if (d == c)
        {
            break;
        }
    }
    a = a + 1;
}
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 2
    That depends on the actual circumstances. The example you give is easily simplified to `while (x != y) {// do stuff}`. But I assume, your real case is more comlicated. – user2390182 Sep 27 '19 at 06:27
  • I'll post the actual circumstances, in a comment edit. – Sam Thompson Sep 27 '19 at 06:38
  • Efficiency of the outer loop hardly matters if there's a lot of code inside it. You don't need `else` after `break`, and therefore you don't need the code controlled by the `else` to be inside `{}` and indented.You also don't need to test `d == c` after having already tested `d < c` and `d > c`. Attention to this sort of thing alone would simplify the look of your code considerably. – user207421 Sep 27 '19 at 06:51

3 Answers3

1

Is there a way to write a loop statement without the loop checking for a condition at all? Is there a more efficient way to write an infinite loop other than while(true)?

You can write an infinite loop in multiple ways, but they are all equivalent. Neither is really more efficient than the others:

  1. while (true) { ... }
  2. do { ... } while (true);
  3. for (;;) { ... }

Depending on the actual code, it may make sense to reverse the "break-loop-logic" into "continue-loop-logic", as in:

boolean continueLoop;
do {
    continueLoop = false;

    // ... do stuff ...

    if ( some condition ) {
        continueLoop = true;
    }

    // ... do stuff ...

} while (continueLoop);
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
0

For your particular example, you may move the logic for breaking in the if statement to the while condition:

while (x != y) {
    // do stuff
}

In fact, if your original while loop had multiple conditions for breaking, you might be able to move them all to the while condition, e.g.

while (!cond1 && !cond2 ... ) {
    // execute
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 2
    While that second example may work, I often find that in such cases, the values of `cond{i}` change during loop body execution, often before they are evaluated to trigger a break. – user2390182 Sep 27 '19 at 06:30
  • 2
    @schwobaseggl I'm speculating of course, because we don't know the OP's actual code (and get ready when we find out it is much more complex). I agree with your comment though. – Tim Biegeleisen Sep 27 '19 at 06:31
0

Yes there are a lot ways you can do this. For example you can declare a variable outside a loop put a condition based on variable value and reset that variable inside a loop, hence loop will run infinitely without checking internal conditions.

Read this for examples :- https://en.wikipedia.org/wiki/Infinite_loop

manish jha
  • 43
  • 6