SIGN UP MEMBER LOGIN:    
ARTICLE

Goto statement in C#

Posted by Hirendra Sisodiya Articles | C# Language July 17, 2010
How to use go to statement in C#.
Reader Level:

Goto  statement  In  C#

The goto is C#  unconditional jump statement. When encountered, program flow jumps to the location specified by the goto.

The goto requires a label of operation. A label is a valid C# identifier followed by colon.

There are different-different ways for using Goto statement such as:

  1. We can write code for loop with the help of goto statement

            int x = 1;

        Loop:

            x++;

            if (x < 100)

            {

                goto loop;

            }

  1. The goto can also be used to jump to a case or default statement in a switch

         Here is the example

string Fruit = "Apple";

            switch (Fruit)

            {

                case "Banana":

                    MessageBox.Show(Fruit + " is the delecious fruit");

                    break;

                case "Chair":

                    MessageBox.Show(Fruit + " is the delecious fruit");

                    break;

                case "Apple":

                    goto case "Banana";

                case "Table":

                    goto case "Chair";

                default:

                    MessageBox.Show("Select valid option");

                    break;

            }

          

In this case, case and default statements of a Switch are labels thus they can be targets of a goto. However, the goto statement must be executed from within the switch. that is we cannot use the goto to jump into switch statement .

  1. With while, we can understand easily with this example

int a = 0;

            while (a < 10)

            {

                if (a == 5)

                    goto cleanup;

            }

        cleanup:         
            
MessageBox.Show(a);

Login to add your contents and source code to this article
share this article :
post comment
 

can you create a simple c# program that uses reference and output parameters,boxing and unboxing,is and as operators?pls....

Posted by jbryl jay Mar 01, 2011

Really superb ... plz continue..becz your article helped me a lot..thanks

Posted by amal es Jan 04, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor