Monday, December 15, 2008

goto Statement:

Definition:

A labeled statement permits a statement to be prefixed by a label, and goto statements can be used to transfer control to a labeled statement.

This statement transfers control to a labeled statement unconditionally. In C#, any statement can be labeled. A colon follows a statement label, and a label identifier follows the goto keyword. The goto statement transfers control to the statement named by the label identifier.

If you overuse the goto statement, it can create code that is hard to read and follow. In general, it's a good practice to avoid using goto altogether by restructuring the code in a way that allows you to get around using the goto statements.

Example:

static void Main(string[] args)

{

goto A;

B: Console.WriteLine("Hello Hems");

Console.ReadLine();

return;

A: Console.WriteLine("Hello Dulip");

Console.ReadLine();

goto B;

}

Output:

Hello Dulip;
Hello Hems;

No comments: