In this chapter you will learn:
- What is goto statement in C#?
- What is the functionality of goto statement?
- How to use goto statement in C# programming?
The goto statement is a jump statement that controls the execution of the program to another segment of the same program. You create a label at anywhere in the program then can pass the execution control via the goto statements.
Example:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace goto_statements { class Program { static void Main(string[] args) { string name; label: //creating label with colon(:) Console.WriteLine("Enter your name: "); name = Console.ReadLine(); Console.WriteLine("Welcome {0}", name); Console.WriteLine("Press Ctrl + C for Exit\n"); goto label; //jump to label statement } } }
Note: To terminate the program press Ctrl+C
Output
Enter your name :
Steven
Welcome Steven
Press Ctrl + C for ExitEnter your name :
Clark
Welcome Clark
Press Ctrl + C for ExitEnter your name :
Steven Clark
Welcome Steven Clark
Press Ctrl + C for Exit
Enter your name : __
Summary
In this chapter, you learned what goto statement is in C# and how to use it in programming. In next chapter you will learn about break statement in C#.