In Focus

Difference Between Break Statement and Continue Statement in C#

In this blog you will learn about when we use break statement and when continue statement. There is a very minor difference in both statement.

Difference b/w Break and continue statement
 
Using break statement,you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.
 
Eg. Break Statement
  1. using System;    
  2. using System.Collections;    
  3. using System.Linq;    
  4. using  System.Text;    
  5.       
  6. namespace break_example {    
  7. Class  brk_stmt {    
  8.       
  9.  public static void main(String [] args) {    
  10.  {    
  11.   forint i=0; i<=5; i++) {    
  12.   if ( i==4) {    
  13.   break;    
  14.   }    
  15.     Console.ReadLine( “The number is”+i );    
  16.     }    
  17.    }    
  18.   }    
  19. }  
Output
 
The number is 0;
The number is 1;
The number is 2;
The number is 3;
 
Eg. Continue Statement
  1. using System;    
  2. using System.Collections;    
  3. using System.Linq;    
  4. using  System.Text;    
  5.       
  6. namespace continue_example {    
  7.     Class  cntnu_stmt     
  8.     {    
  9.         public static void main(String [] args)    
  10.         {    
  11.             forint i=0; i<=5; i++)     
  12.             {    
  13.                  if ( i==4) {    
  14.                      continue;    
  15.                  }    
  16.                  Console.ReadLine( “The number is”+i);    
  17.             }    
  18.         }    
  19.    }    
  20. }   
Output
 
The number is 0;
The number is 1;
The number is 2;
The number is 3;
The number is 5;