C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used inside the for loop and switch statements. If you are using nested loop into your program then break statement will break only inner loop. You can’t use break outside any loop or switch statements . Let's see a simple example of break statement:

using System;
public class BreakExample{
            public static void Main (string[] args)
            {
                        int i;
            for(i=0;i<10;i++)
            {
                        Console.WriteLine(i);
                        if(i==6)
                                    break;
            }
                        Console.WriteLine("loop break i="+i);
            }
}
Output
0
1
2
3
4
5
6
loop broke i=6
C# Continue statement: Continue statement is used to skip the remaining code of the loop for a specified condition and continue the flow control from next iteration. It is used if you don’t want to run a certain part of loop for a separate condition. Let's see a simple example of continue statement
using System;
public class ContinueStatementExample
{
public static void main (string[] args)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine(i);
If(i==5) 
Continue;
}
}
}
Output
1
2
3
4
6
7
8
9
10
C# goto statement: Goto statement is also known as a jump statement. It transfers the flow of control somewhere else in the program (where the label is mentioned). It is mainly used to break deeply nested loop or nested switch statements. Using goto statements is mainly avoided since it increases program complexity.
C# goto statements Example:
using System;  
public class GotoExample  
{  
public static void Main(string[] args)  
{  
ineligible:  
Console.WriteLine("You are not eligible to vote!");  
Console.WriteLine("Enter your age:\n");  
int age = Convert.ToInt32(Console.ReadLine());  
if (age < 18){  
goto ineligible;  
}  
else  
{  
Console.WriteLine("You are eligible to vote!");   
}  
}  
}
Output
You are not eligible to vote!
Enter your age:
17
You are not eligible to vote!
Enter your age:



ADVERTISEMENT
ADVERTISEMENT