C Tutorial (16) : Break & Continue in Loops

The break and continue statements let you control loops for those special occasions when you want to quit a loop early or repeat a loop sooner than it would normally repeat.

The break statement rarely, if ever, appears on a line by itself. Typically, break appears in the body of an if statement. The reason for this will be made clear shortly. Here is the format of break:

break;

Note : break is easy, isn’t it? Yep, not much to it. However, keep in mind that break usually resides in the body of an if. In a way, if is the first part of almost every break.

break always appears inside a loop. The purpose of break is to terminate the current loop. When a loop ends, the code following the body of the loop takes over. When break appears inside a loop’s body, break terminates that loop immediately, and the rest of the program continues.

Here is a for loop that normally would print 10 numbers. Instead of printing 10, however, the break causes the loop to stop after printing 5 numbers.

for (i=0; i < 10; i++)
{
  printf("%d ", i);
  if (i == 4)
  {
     break;
  }
}
// Rest of program would follow.

Note : break simply offers an early termination of a while, do-while, or for loop. break can’t exit from if, which isn’t a loop statement.

 

c_break

Continue

Whereas break causes a loop to break early, continue forces a loop to continue early.  Depending on the complexity of your for, while, or do-while loop, you might not want to execute the entire body of the loop every iteration. continue says, in effect, “C, please ignore the rest of this loop’s body this iteration of the loop. Go back up to the top of the loop and start the next loop cycle.”

Note : The word iteration is a fancy computer name for the cycle of a loop. Programmers sometimes think they will keep their jobs if they use words that nobody else understands 🙂

The following program shows off continue nicely. The program contains a for loop that counts from 1 to 10. If the loop variable contains an odd number, the message I’m rather odd…prints, and the continue instructs C to ignore the rest of the loop body because it prints Even up! for the even numbers that are left.

// Loops through the numbers 1 through 10
for (i = 1; i <= 10; i++)
{
  if ((i%2) == 1) // Odd numbers have a remainder of 1
  {
    printf("I'm rather odd...\n");
    // Will jump to the next iteration of the loop 
    continue;
  }
  printf("Even up!\n");
}

 

Note : As with break, continue is rarely used without a preceding if statement of some kind. If you always wanted to continue, you wouldn’t have entered the last part of the loop’s body. You want to use continue only in some cycles of the loop.

 

For Loop < Prev                                                                        Next >  Switch & Case

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s