The goto statement is used when you need to jump to a particular code segment.
It’s similar to the goto statement in visual basic or C++.
In the following code, if an item of array is found, the control goes to the level found and skips all code before that.
Most programmers avoid using the goto statement, but you may find a rare need for it. One such occasion is the use of fall-through on a switch statement.
Fall- thought is the ability for the control flow to fall from one case statement directly into another by leaving out the break statement.
In C#, fall-though in a switch statement is not allowed as it was in C++. However, if you explicitly tell the switch statement to go to the next label, it will perform a jump to the next case, essentially carrying out the same function as a fall-through. Note that when using a go to in a case statement, you don’t have to provide a break (in all other cases, a break statement is mandatory). in this is bill” and “sometimes I’m called William” are displayed on the screen:
Console.WriteLine("What is your name? "); string name = Console.ReadLine(); switch(name) { case "Bill": Console.WriteLine("My name is Bill."); goto case "William"; case "William": Console.WriteLine("Sometimes I’m called William."); break; case "Anne": Console.WriteLine("My name is Anne. "); break; default: break; }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|