Essential C# Looping  Suggest an edit

break

Sometimes loop condition should be checked in the middle of the loop. The former is arguably more elegant than the latter:

for (;;)
{
    // precondition code that can change the value of should_end_loop expression

    if (should_end_loop)
        break;

    // do something
}

Alternative:

bool endLoop = false;
for (; !endLoop;)
{
    // precondition code that can set endLoop flag

    if (!endLoop)
    {
        // do something
    }
}

Note: In nested loops and/or switch must use more than just a simple break.


Table Of Contents
22 Enum
25 GUID
28 Looping
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close