Jump Statements in C#.net

Posted by Naveenmanam under C# category on | Views : 5848
C# jump statement

C# jump statement s:- break, continue, goto, return, and throw. All jump
statements obey sensible restrictions imposed by try statements . First, a jump out of a try block always executes the try's finally block before
reaching the target of the jump. Second, a jump can't be made from the inside to the outside of a
finally block.
Break statement


The break statement transfers execution from the enclosing while loop, for loop, or switch
statement block to the next statement block:

int x = 0;
while (true)
if (x>5)
break; // break from the loop

continue statement


The continue statement forgoes the remaining statements in the loop and makes an early start
on the next iteration:

int x = 0;
int y = 0;
while (y<100)
if ((x%7)==0)
continue; // continue with next iteration

goto statement:


goto statement-label;

goto case-constant;
The goto statement transfers execution to another label within the statement block. A label
statement is just a placeholder in a method:

int x = 4;
if (x==5)
goto start;

The goto case statement transfers execution to another case label in a switch block .

return statement:

return expression?;

The return statement exits a method. If the method is non-void, it must return an expression of
the method's return type:

int CalcX(int a)
int x = a * 100;
return x; // return to the calling method with value

throw statement:

throw exception-expression?;

The throw statement throws an Exceptionto indicate that an abnormal condition has occurred

if (w==null)
throw new Exception("w can't be null");

lock statement:


lock (expression)

The lock statement is actually a syntactic shortcut for calling the Enter and Exit methods of
the Monitor class .

using statement

using (declaration-expression)

Many classes encapsulate non-memory resources, such as file handles, graphics handles, or
database connections. These classes implement System.IDisposable, which defines a single
parameterless method named Dispose that is called to clean up these resources. The using
statement provides an elegant syntax for declaring, then calling, the Dispose method of
variables that implement IDisposable. For example:

using (FileStream fs = new FileStream (fileName, FileMode.Open))
do something with fs
This is precisely equivalent to:
FileStream fs = new FileStream (fileName, FileMode.Open);
try {

} finally {
if (fs != null)
((IDisposable)fs).Dispose( );

Comments or Responses

Login to post response