Cloud Training (Online)




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 .