IV-Jump Statement
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. There are five keywords in the Jump Statements:
- break
- continue
- goto
- return
- throw
IV.1. break statement
The break statement is used to terminate the loop or statement in which it present. After that, the control will pass to the statements that present after the break statement, if available. If the break statement present in the nested loop, then it terminates only those loops which contains break statement.
IV.2- Continue Statement
This statement is 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. Basically, it skips its following statements and continues with the next iteration of the loop.
IV.3- 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.
IV.4- Goto Statement
This statement is used to transfer control to the labeled statement in the program. The label is the valid identifier and placed just before the statement from where the control is transferred.
IV.5. throw statement
This is used to create an object of any valid exception class with the help of new keyword manually. The valid exception must be derived from the Exception class.
Example:
// C# Program to illustrate the 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 );
}
}
}
Output:
Exception Message
0 Comments