CodingBison

C provides methods for an application to be more selective when running inside a loop. There are two specific tasks that an application might wish to do when present in a loop: skip some of the records and break out of the loop. C provides two keywords, "continue" and "break" that allows a program to skip a record or to break out of the loop respectively.

Here is a representative flow-chart that highlights the behavior of these statements. When condition for continue is met, then we skip the rest of the tasks in that iteration of the loop and continue the next iteration of the loop. On the contrary, when the condition for break is met, then we skip the loop altogether and break out of it.



Figure: Continue and Break for a while loop

For cases where we have multiple levels of loops (meaning, a loop running inside another loop), the "break" statement merely exists from the current loop level. The immediate outer loop surrounding the current loop continues to run. Similar behavior holds true for the continue statement as well.

With that, let us go through an example that uses these keywords. In the interests of simplicity, we use only a "for" loop. But, these keywords would apply equally well to the remaining loop variants of while and do-while.

In this example, we need to print an inventory of various paintings and if a particular painting does not have any availability, we skip printing that record. To do so, we take the help of the "continue" keyword. Another use-case would be when we need to look for a specific painting, let us say, "Mona Lisa" and we want to see its availability. We can run in a loop and once we find "Mona Lisa", we can break out of the loop using the "break" keyword.

 #include <stdio.h>

 #define THE_LAST_SUPPER 1001
 #define MONA_LISA       1002
 #define POTATO_EATERS   1003
 #define CYPRESSES       1004

 #define MAX_PAINTINGS 	4

 int main () {
     int counter;
     int painting_array[MAX_PAINTINGS][2] = {
 	{THE_LAST_SUPPER, 100},
 	{MONA_LISA, 	   10},
 	{POTATO_EATERS,     0},
 	{CYPRESSES,        11},
     };

     printf("Let us print inventory \n"); 
     for (counter=0; counter < MAX_PAINTINGS; counter++) {
         if (painting_array[counter][1] == 0) {
         /* Means, the painting is not available */
             continue;
         }
         printf("[continue loop] i: %d, painting_id: %d Availability: %d \n", 
                 counter, 
                 painting_array[counter][0],
                 painting_array[counter][1]);
     }

     printf("Let us search for Mona Lisa \n"); 
     for (counter=0; counter < MAX_PAINTINGS; counter++) {
         printf("[break loop] i: %d, painting_id: %d Availability: %d \n", 
                     counter, 
                     painting_array[counter][0],
                     painting_array[counter][1]);
         if (painting_array[counter][0] == MONA_LISA) { 
             break;
         }
     }
     return 0;
 }

The first "for" loop skips paintings that are not available. The second "for" loop searches for "Mona Lisa" and breaks out of the loop when it finds the painting record for "Mona Lisa".

Here is the output:

 $ gcc continue-break-loops.c -o cbloops
 $ 
 $ ./cbloops 
 Let us print inventory 
 [continue loop] i: 0, painting_id: 1001 Availability: 100 
 [continue loop] i: 1, painting_id: 1002 Availability: 10 
 [continue loop] i: 3, painting_id: 1004 Availability: 11 
 Let us search for Mona Lisa 
 [break loop] i: 0, painting_id: 1001 Availability: 100 
 [break loop] i: 1, painting_id: 1002 Availability: 10 




comments powered by Disqus