#1,126 – Rewriting a Loop to Avoid continue Statement

The continue statement allows jumping to end of the body of a loop, skipping the rest of the code within the loop.  Either the next iteration of the loop executes, or the code following the loop statement executes.

            List<Dog> myDogs = new List<Dog>
            {
                new Dog {Name = "Kirby", Age = 5},
                new Dog {Name = "Jack", Age = 17},
                new Dog {Name = "Ruby", Age = 2}
            };

            foreach (Dog d in myDogs)
            {
                if (d.Age > 15)
                    continue;

                Console.WriteLine("{0} doing a trick", d.Name);
                d.DoATrick();
            }

1126-001
In most cases, you can rewrite the body of a loop and avoid using a continue statement by using an if statement.  The end result is generally more readable.

            foreach (Dog d in myDogs)
            {
                if (d.Age <= 15)
                {
                    Console.WriteLine("{0} doing a trick", d.Name);
                    d.DoATrick();
                }
            }

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

7 Responses to #1,126 – Rewriting a Loop to Avoid continue Statement

  1. Pingback: Dew Drop – June 26, 2014 (#1804) | Morning Dew

  2. I take your point for very simple loops, but as soon as you get in to more complex tests, the readability can drop off as you end up with nested if statements. I prefer to do as much testing at the start of the loop and break out to the next iteration with a continue. That allows for a series of guard statements (preferably well named method calls) that execute before you get in to the main processing in the loop.

    This reduces the nesting of if statements (which can be a source of confusion – the ‘how did I get here?’ question and reduces the indentation of the code making it more readable.

    Just my opinion.

    • Sean,

      I’m with Steve here. I tend to prefer to test for escape conditions for break/continue/return/yield as soon as possible. The problem is that when potentially dealing with a large block of code I find this more readable. In fact this rule holds for methods as well and goes to providing a consistency within the code. At the start of a code block test for error/escape conditions then continue with the code.

      Imagine in your sample if it was a method call where you didn’t know if myDogs had been initialized.

      You wouldn’t do:
      if(myDogs != null && myDogs.Count > 0)
      {
      /* what if there was 30 or more lines of code here? */
      /* do foreach loop */
      }
      else
      {
      /* handle error condition, throw ArgumentNullException, InvalidOperationException ? */
      }

      You would do:
      if(myDogs == null || myDogs.Count == 0)
      { /* handle error condition, throw ArgumentNullException, InvalidOperationException ? */ }

      /* what if there was 30 or more lines of code here? */
      /* do foreach loop */

      especially when the code following the conditional would be exceedingly long.

      Or maybe you wouldn’t 🙂 I just find the second example far more readable.

      Thanks,
      Brian

  3. Shane says:

    If you use reSharper with the IF statement way it would recommend you use the Continue method instead to reduce nesting. I would say the reduced nesting is more readable.

  4. Steve says:

    This is a simple example. I think the continue statement is good to be used in a complicated loop. Anyway, thanks for the post.

  5. Jim Joseph says:

    For this example, I would first reduce the collection and then iterate it. For example,

    var youngDogs = dogs.Where(d => d.Age <= 15);

    foreach(var youngDog in youngDogs)
    {
    // no more IF or CONTINUE
    }

Leave a comment