While working through the code i found wondering myself what exactly is the "continue" keyword means. Although its usage is pretty less these days but it is better to know stuff than not.
So, the below sample code will explain the difference between Continue & Break Statement in C#. You can read the comment or see the Output to understand it.
The code can also be found at My Code Fiddle (You can run it, to see it)
Output:
1
2
3
5
Loop A is Completed
1
2
3
Loop B is Completed
So, the below sample code will explain the difference between Continue & Break Statement in C#. You can read the comment or see the Output to understand it.
The code can also be found at My Code Fiddle (You can run it, to see it)
using System; public class Program { public static void Main() { for(int i = 1; i < 6; i++) { //continue is like a special break statement in which // instead of jumping off loop block, //the current loop iteration(n) is treated as completed. //The execution is then forced to start with the next iteration(n+1) loop. if(i == 4) continue; Console.WriteLine(i); } Console.WriteLine("Loop A is Completed"); Console.WriteLine(); for(int i = 1; i < 6; i++) { //The break statement will make the execution jump off loop block. //No more iteration(n+1) loop will be respected. if(i == 4) break; Console.WriteLine(i); } Console.WriteLine("Loop B is Completed"); } }
Output:
1
2
3
5
Loop A is Completed
1
2
3
Loop B is Completed
No comments:
Post a Comment