Subscribe to our YouTube Channel for more Videos

Break vs. continue in C#


Within a for loop or a while loop in C# , you can use break or continue keyword .

What is the different between break and continue in C# ?

break keyword will cause the loop to exit .

Example

for(int i = 0; i < 5; i++){
   if(i==1) break;
   ProcessGK(i);
}

In the above example , if i = 1 , then the loop will exit.

continue keyword will just skip the current iteration in the loop and continues to the next one.

for(int i = 0; i < 5; i++){
   if(i==1) continue;
   ProcessGK(i);
}

In the above example , if i = 1 , then the current iteration will be skipped and loop continues for i = 2 .

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.