Jump Statements C# Help

C# provides a number of statements that allow you to jump immediately to another line in the program. The first of these is, of course, the notorious goto statement.

The goto Statement

The goto statement allows you to jump directly to another specified line in the program, indicated by a label (this is just an identifier followed by a colon):

go to Labell;
Console.WriteLine(“This won’t be executed”);
Labell:
Console.WriteLine(“Continuing execution from here’);

A couple of restrictions are involved with go to. You can’t jump into a block of code such as a for loop,:’

The reputation of the goto statement probably precedes it, and in most circumstances, its use is sternly. . frowned upon. In general, it certainly doesn’t conform to good object-oriented programming practice. However, there is one place where it is quite handy: jumping between cases in a switch statement, particularly because C#’s switch is so strict on fall-through..

The break Statement

You have already met the break statement briefly – when you used it to exit from a case in a switch statement. In fact, break can also be used to exit from for, for each, while, or do … while loops. control will switch to the statement immediately after the end of the loop. -.If the statement occurs in a nested loop, control will switch to the end of the innermost loop. If the break  occur outside of a switch statement or a loop, a compile-time error will occur.

The continue Statement

The continue statement is similar to break, and must also be used within a for, for each, while, or do … while loop. However, it exits only from the current iteration of the loop, meaning, that execution will restart at the beginning of the next iteration of the loop, rather than outside the loop altogether ..

The return Statement

The return statement is used to exit a method of a class, returning control to the caller of the method. If the method has a return type, return must return a value of this type; otherwise if the method returns void, you should use return without an expression.

Posted on October 27, 2015 in C# Basics

Share the Story

Back to Top
Share This