HomeCSharpBreak vs. continue in C#

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...