Wednesday, May 23, 2012

C# goto Statement


C# goto Statement

The goto statement in c# is used to transfer control to a predefined label statement, it can be used to skip execution in a loop on reaching a condition, if a goto statement is reached then the code execution is transferred to the location where the goto label is pointing to.

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        goto SkipLoop;
    }
    //
    Response.Write(i.ToString() + "<br>");
}
//
SkipLoop:
Response.Write("Statement after the for Loop");

In the above example when i values is equal to 5 the goto statement is executed and code execution is transferred to SkipLoop label statement, the output in this case will be

1
2
3
4
Statement after the for Loop


That’s it, we have evaluated the use of the goto statement.

Related Post

Search Flipkart Products:
Flipkart.com

No comments: