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 .

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...