Difference between break and continue in c#

  • break : key word used when we want to terminate code execution or terminate execution of loop let see below example and understand.
 for (int k = 0; k < 10; k++)    
 {      
            if (k == 5)    
       
     {    
          
      break;    

            }    

        }  
  As seen in above code for loop will terminate execution when value of k reach five(5).
As we can see k value will generate up-to 10 so loop will run 10 cycle but as we use break key word loop will run up-to 5 cycle and complier terminate loop execution.
 
  • continue :  key word used when we want to keep loop execution continue


for (int k = 0; k < 10; k++)    
 {      
            if (k == 5)    
       
     {    
          
      break;    

            }
elseif(k < 5)
{
continue;
}    

        }


Categories:

0 comments:

Post a Comment