C# Jump Statements (Break, Continue, Goto, Return and Throw) (original) (raw)
Last Updated : 11 Jan, 2025
In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.
Types of Jump Statements
There are mainly five keywords in the Jump Statements which are mentioned below:
- break
- continue
- goto
- return
- throw
1. break statement
The break statement is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available. If the break statement is present in the nested loop, then it terminates only those loops which contain the break statement.
**Flowchart
**Example:
C# `
// Use of break statement using System;
class Geeks { static public void Main() { // GeeksforGeeks is printed only 2 times // because of break statement for (int i = 1; i < 4; i++) { if (i == 3) break;
Console.WriteLine("GeeksforGeeks");
}
}
}
`
Output
GeeksforGeeks GeeksforGeeks
2. continue statement
**continue statement used to skip over the execution part of the loop on a certain condition. After that, it transfers the control to the beginning of the loop. It skips its following statements and continues with the next iteration of the loop.
**Flowchart
**Example:
C# `
// Use of continue statement using System;
class Geeks { public static void Main() { // This will skip 4 to print for (int i = 1; i <= 5; i++) { // if the value of i becomes 3 then // it will skip 3 and send the // transfer to the for loop and // continue with 5 if (i == 3) continue;
Console.WriteLine(i);
}
}
}
`
3. goto statement
**Goto statement is used to transfer control to the labeled statement. The label is the valid identifier and is placed just before the statement from where the control is transferred.
**Key points:
- **Syntax: we can write goto followed by a label name that you’ve defined in your code.
- **Label: A label is just a name followed by a colon (e.g., labelName).
- **Usage: It’s generally not recommended because it can make your code harder to read and understand and sometimes create ambiguity in the program
**Flowchart
**Example:
C# `
// Use of goto statement using System;
class Geeks { static public void Main() { int number = 20;
// Switch case statement
switch (number)
{
case 5:
Console.WriteLine("case 5");
break;
case 10:
Console.WriteLine("case 10");
break;
case 20:
Console.WriteLine("case 20");
// goto statement transfers
// control to case 5
goto case 5;
default:
Console.WriteLine("No match found");
break;
}
}
}
`
4. return statement
This statement terminates the execution of the method and returns the control to the calling method. It returns an optional value. If the type of method is void, then the return statement can be excluded.
**Example:
C# `
// Use of return statement using System;
class Geeks { // creating simple addition function static int Addition(int a) { // add two value and // return the result of addition int add = a + a;
// using return statement
return add;
}
static public void Main()
{
int number = 2;
// calling addition function
int result = Addition(number);
Console.WriteLine("The addition is {0}", result);
}
}
`
5. throw statement
**throw statement is used to raise exceptions in C#. When an error occurs or a specific condition is met, weuse throw to signal that something unexpected happened. It is used as jump statement we can create custom exception and throw it when the condition is not met accordingly.
**Example:
C# `
// Use of throw keyword using System;
class Geeks { // taking null in the string static string sub = null;
// method to display subject name
static void displaysubject(string sub1)
{
if (sub1 == null)
throw new NullReferenceException("Exception Message");
}
// Main Method
static void Main(string[] args)
{
// using try catch block to
// handle the Exception
try {
// calling the static method
displaysubject(sub);
}
catch(Exception exp) {
Console.WriteLine(exp.Message );
}
}
}
`