#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();
                }
            }
Advertisements

#1,125 – Use of break and continue Statements

You can use the break statement to break out of a loop or switch statement, resuming execution at the endpoint of the enclosing statement.

You can use break within a switch statement or within while, do, for or foreach loops.

You can use the continue statement to jump unconditionally to the endpoint of the body of statements enclosed within a loop.  This will either cause execution to continue with the next iteration of the loop or will continue execution after the loop if the final iteration was being executed.

You can use continue within a while, do, for or foreach loop.

#167 – Use continue to Jump to Next Iteration of for Loop

Within the body of a for loop, you can use the continue statement to stop executing the current iteration of the loop and continue with the next iteration.

            // Compute/display some factorials
            for (long i = 1; i <= 20; i++)
            {
                // I'm superstitious--avoid 13!
                if (i == 13)
                    continue;

                Console.WriteLine(string.Format("Factorial of {0} is {1}", i, FactorialOf(i)));
            }

#161 – Use continue to Jump to Next Iteration of While Loop

Within a while loop, you can use the continue statement to stop executing the current iteration of the loop and continue with the next iteration.

            while (booksIWantToRead > 0)
            {
                SelectNextBook();
                BuyNextBook();
                bool seemsInteresting = ReadBackCover();
                if (!seemsInteresting)
                    continue;    // continue with next book
                ReadTheBook();
                TellFriendsAboutBook();
                booksIWantToRead--;
            }

#156 – Using break and continue in foreach Loops

When iterating through array elements using the foreach statement, you can use the break statement to break out of the loop–when break is encountered, control is transferred to the code immediately following the loop and no more array elements will be processed.

            foreach (Person p in folks)
            {
                Console.WriteLine("{0} {1}", p.FirstName, p.LastName);

                // If we find Elvis, stop the presses
                if (p.FirstName == "Elvis")
                    break;
            }

The continue statement, when encountered in a foreach loop, causes control to transfer back to the top of the loop and move on to the next element.

            foreach (Person p in folks)
            {
                // Don't print out info for any Adolfs; move to the next person
                if (p.FirstName == "Adolf")
                    continue;

                Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
            }