2

Lets take the below code snippet:

int i = 0;
while ( i <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }
    i++;
}

What changes do I have to make in the code to avoid infinite loop?

Jeankowkow
  • 814
  • 13
  • 33
Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39
  • 2
    `i++; if (i == 9) { continue;}` ? – assylias Jan 17 '13 at 19:35
  • 7
    Your question is entirely confusing. Yes, when you `continue` it goes back to the top of the loop - including checking the condition. If you don't want that behaviour, don't use `continue`... what are you really trying to achieve? – Jon Skeet Jan 17 '13 at 19:35
  • 2
    this is basically writing a `for` loop using `while`, and then intentionally breaking the control variable for a specific condition. Typically you'd use continue to skip the processing for a specific condition, rather than skip incrementing the control variable – Gus Jan 17 '13 at 19:36
  • 1
    continue works the same in the for loop. The reason this wouldn't be an infinite loop in a for loop is because, in the for loop, i is incremented with every iteration. So even though it would continue when i == 8, the next iteration would make i = 9. – Stoop Jan 17 '13 at 19:36
  • 1
    I find that the use of `continue` generally confuses loops, so would want to write it in a clearer manner anyway. – Tom Hawtin - tackline Jan 17 '13 at 19:37
  • 1
    Solution: don't write bad code on purpose. – Hovercraft Full Of Eels Jan 17 '13 at 19:40
  • A related question is http://stackoverflow.com/questions/6414/c-sharp-loop-break-vs-continue – Mihai8 Jan 17 '13 at 19:41
  • consider to replace while loop with for loop with iteration inside the statement, in the bad case even to do while – Roman C Jan 17 '13 at 19:59
  • lol​‌‌‍‍‍‌‌​‌‭​‌‌​‭‌‌​ – cmarangu Oct 29 '19 at 17:09

4 Answers4

13

Do your increment at the beginning instead of at the end:

int i = -1;
while ( i <= 10 )
{
    i++;
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }

    // Presumably there would be some code here, or this doesn't really make much sense
}

Or, depending on the language, you could do it right in the while statement (keeping in mind operator precedence in whether you choose i++ or ++i)

int i = 0
while ( i++ <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }

    // Presumably there would be some code here, or this doesn't really make much sense
}

I would question the use of a while loop for this kind of structure though. If you want to use a counter in a loop, a for loop is typically more appropriate.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
4

instead of a quickfix solution, lets see your code for a minute and step through it line by line:

int i = 0;
while ( i <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }
    i++;
}

i at first is 0, its less than 10 and hence it enters the loop, prints 0 and increments to 1. Then i becomes 2, 3, 4, .. 8

When it becomes equal to 8, instead of incrementing, it pops back to the beginning of the loop, printing 8 again.. checks for the value of i(which is 8) and continues again, printing 8.. and it will keep doing this until eternity.

Hence, increment the number before testing and it will work as expected.

Change your code to something like this

int i = 0;
while ( i <= 10 )
{
    if ( i != 8 )
    {
        System.out.println(i);
    }
    i++;
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

I like Eric Petroelje's answer. I suggest doing something like this:

if (++i >= 8) continue;

In addition, these days compilers are good enough to warn you about this as a possible infinite loop. There are also code analysis tools that will also detect this for you.

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
0

While this is not code I would ever recommend using in most cases, it does serve the purpose:

int i = 0;
while ( i <= 10 )
{
  Console.WriteLine(i.ToString());
  if ( i == 8 )
  {
    // Do some work here, then bail on this iteration.
    goto Continue;
  }

Continue:  // Yes, C# does support labels, using sparingly!
  i++;
}
Erik
  • 12,730
  • 5
  • 36
  • 42