CSharp/C# Tutorial - C# Jump Statements






The C# jump statements are break, continue, goto, return, and throw.

The break statement

The break statement ends the execution of the body of an iteration or switch statement:


int x = 0; 
while (true) {
    if (x++ > 5) 
        break ; // break from the loop 
} 
// execution continues here after break 





The continue statement

The continue statement skips the remaining statements in a loop and makes an early start on the next iteration.

The following loop skips even numbers:


for (int i = 0; i < 10; i++) { 
    if ((i % 2) == 0){
        continue; // continue with next iteration
    }
    Console.Write (i + " "); 
} 

The goto statement

The goto statement transfers execution to another label within a statement block.

The form is as follows:


goto statement-label; 

Or, when used within a switch statement:


goto case case-constant; 

A label is a placeholder that precedes a statement, denoted with a colon suffix.

The following iterates the numbers 1 through 5, mimicking a for loop:


int i = 1; 

startLoop: 
if (i <= 5) {
   Console.Write (i + " ");
   i++; 
   goto startLoop; 
} 




The return statement

The return statement exits the method.


decimal AMethod (decimal d) {
   decimal p = d * 100m;
   return p; 
} 

A return statement can appear anywhere in a method.