dmilicev

for loop with continue and break v1.c

Oct 12th, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. /*
  2.  
  3.     for loop with continue and break v1.c
  4.  
  5.  Problems arise if we change the loop counter within the loop itself !
  6.  
  7.  See version 2 for better solution :
  8.  
  9.     for loop without continue and break v2.c
  10.  
  11. */
  12.  
  13. #include "stdio.h"
  14.  
  15. int main(void)
  16. {
  17.     int i, n=15, min=5, max=10, counter=0;
  18.  
  19.     printf("\n min = %d , max = %d , n = %d \n\n", min, max, n);
  20.  
  21.     for (i=0; i<n; i++)
  22.     {
  23.         while( (i<min || i>max) && i<n )
  24.         {
  25.             printf("\n i = %2d , continued \n",i);
  26.  
  27.             i++;        // go to next i
  28.             continue;   // do nothing
  29.         }
  30.  
  31.         if(i>n-1)       // exit from for loop
  32.             break;
  33.  
  34.         counter++;
  35.         printf("\n i = %2d      counter %2d.        * \n", i, counter);
  36.     }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment