Break Vs Continue in C#

Break and continue is a very basic concept of any programming language and I think it is supported by almost all popular languages e.g. C++, C#, Java, JavaScript, etc. I know that most of us know about it very well but for fresher guys its little bit confusing. So, I am explaining here so that even fresher can understand and use it easily.

Break (breaks the loop/switch)

Break statement is used to terminate the current loop iteration or terminate the switch statement in which it appears

Break statement can be used in the following scenarios:

  • for loop (For loop & nested for loop and Parallel.for)
  • foreach loop (foreach loop & nested foreach loop and Parallel. foreach)
  • While (while loop & nested while loop)
  • Do while (do while loop and nested while loop)
  • Switch case (Switch cases and nested switch cases)

Continue (skip the execution of current iteration)

The continue statement is not same as break statement. Break statement breaks the loop/switch whereas continue skip the execution of current iteration only and it does not break the loop/switch i.e. it passes the control to the next iteration of the enclosing while loop, do while loop, for loop or for each statement in which it appears.

Let’s understand the difference via some examples:

  1. List<int> EvenNumberList = new List<int>();    
  2. List<int> OddNumberList = new List<int>();    
  3. List<int> PrimeNumberList = new List<int>();    
  4.   
  5. for (int i = 1; i <= 200; i++)    
  6. {    
  7.     if (i > 100)    
  8.         break;  //I have to check for numbers less than 100    
  9.     if (i % 2 == 0)    
  10.     {    
  11.         EvenNumberList.Add(i);    
  12.         continue//if a number is even then it cannot be odd or prime number so we can skip the current iteration.    
  13.     }    
  14.     else    
  15.     {    
  16.         OddNumberList.Add(i);    
  17.         if (IsPrimeNumber(i))    
  18.         {    
  19.             PrimeNumberList.Add(i);    
  20.         }    
  21.     }    
  22. }   
1. In the above example I am checking if number is greater than 100, then break the loop. No code for the loop will be executed after that.

2. If a number is even then it cannot be odd or prime number so we can skip the current iteration.

We can break multiple for / for each loop using break,
  1. //We can break multiple for /for each loop using break    
  2. bool BreakAllLoops = false;    
  3. for (int i = 0; i < 10; i++)    
  4. {    
  5.     //do something    
  6.     if (BreakAllLoops)    
  7.         break;    
  8.     for (int j = 0; j < 10; j++)    
  9.     {    
  10.   
  11.         //do something     
  12.         if (BreakAllLoops)    
  13.             break;    
  14.         for (int k = 0; k < 10; k++)    
  15.         {    
  16.             //do something    
  17.             if (k == 5)    
  18.             {    
  19.                 break;    
  20.             }    
  21.         }    
  22.     }    
  23. }   
Break statement in foreach, while, do while, Parallel.for, Parallel. Foreach
  1. #region foreach loop break statement               
  2. foreach (var item in collection)    
  3. {    
  4.     //do something    
  5.     break;    
  6. }    
  7. #endregion    
  8.  
  9. #region while loop break statement    
  10. while (true)    
  11. {    
  12.     break;    
  13. }    
  14. #endregion    
  15.  
  16. #region Do while break statement    
  17. do    
  18. {    
  19.     break;    
  20. while (true);    
  21. #endregion    
  22.  
  23. #region Switch case break statement    
  24. int i = 10;    
  25. switch (i)    
  26. {    
  27.     case 1:    
  28.         //do someting;    
  29.         i = 1;    
  30.         break;    
  31.     default:    
  32.         i = 1;    
  33.         break;    
  34. }    
  35. #endregion    
  36.  
  37. #region Parallel.for break statement    
  38.   
  39. Parallel.for(int m = 0; m < 10; m++)    
  40. {    
  41.     break;    
  42.   
  43. }    
  44. #endregion    
  45.  
  46. #region Parallel.foreach break statement    
  47. Parallel.foreach(var item in collection)    
  48. {    
  49.     break;    
  50. }    
  51. #endregion