Continue and break

From Tux
Revision as of 17:58, 23 January 2018 by Williams (talk | contribs) (Created page with "'''<code>Continue</code> and <code>break</code>''' can be used to alter how loops run. <code>break</code> exits the loop immediately, while <code>continue</code> skips the re...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Continue and break can be used to alter how loops run.

break exits the loop immediately, while continue skips the remainder of the statements in the loop and restarts it.

for (int i = 0; i < 10; i++)   // Prints out the values 0 through 9
     cout << i << endl;

for (int i = 0; i < 10; i++)   // Prints out the values 0 through 5
{
     if (i == 6)
          break;

     cout << i << endl;
}

for (int i = 0; i < 10; i++)   // Prints out the values 0 through 4 and 6 through 9
{
     if (i == 5)
          continue;

     cout << i << endl;
}