Jump to content

(C#) goto and labeled statements

Mo5

I'm having a small problem with a code similar to this:

Console.WriteLine("Input number");                string a3 = Console.ReadLine();if (a3 == "1")                {                    goto Label1;                }else if (a3 == "2")                {                    goto Label2;                }            }        Label1:            Console.WriteLine("Label1");        Label2:            Console.WriteLine("Label2");

The problem is that when I input 1 is writes out both "Label1" and "Label2". How do you properly separate labeled statements?

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to comment
Share on other sites

Link to post
Share on other sites

You can put the WriteLine("Label1") and WriteLine("Label2") in seperate functions and just call whatever function you need instead of using goto.

My Current Build: 

Intel i5 3570K @ 4.4GHz 1.11V, Cooler Master Hyper 212 EVO, Asrock Z77 Extreme4, Corsair Vengeance 8GB 1600MHz, Samsung 840 EVO 250GB, Asus GTX 760 DCII Overclocked, Corsair CX600M

Link to comment
Share on other sites

Link to post
Share on other sites

You don't...

 

Agreed. You should endeavour to NEVER use goto at all, it is extremely bad form... there are exceptions to this but they are generally not found in higher level languages.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Agreed. You should endeavour to NEVER use goto at all, it is extremely bad form... there are exceptions to this but they are generally not found in higher level languages.

Avoid that spaghetti code

Link to comment
Share on other sites

Link to post
Share on other sites

Avoid that spaghetti code

Agreed. You should endeavour to NEVER use goto at all, it is extremely bad form... there are exceptions to this but they are generally not found in higher level languages.

eh..

As long as code doesn't lose its visual structure, and it's easy to follow it, I don't see a problem.

Use it if you have to, but before you decide on it check if a function, switch, flag (bool or enum) or even an if-else statement can substitute it.

Once you include it in your code, review it with your friendly rubber duck and see if you don't come up with a solution after listening to yourself one final time.

Lastly, if you work for/with someone, prepare good explanation for its use.

 

P.S Same goes for continue;

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×