C# goto Statement
In C#, the goto
statement is used to transfer control to a labeled statement elsewhere in the program. The labeled statement must be within the same method or block as the goto
statement.
Here is an example that uses the goto
statement:
int i = 0; start: Console.WriteLine(i); i++; if (i < 10) { goto start; }Sourwww:ec.theitroad.com
In this example, the program uses a labeled statement called start
, which is followed by a Console.WriteLine
statement that prints the value of i
to the console. The program then increments i
and checks whether i
is less than 10. If i
is less than 10, the program uses the goto
statement to transfer control back to the start
label, which starts the loop again. This process continues until i
is no longer less than 10, at which point the program continues executing the code that follows the loop.
Note that the use of goto
is generally discouraged in C# because it can make the code harder to read and understand. In most cases, it is better to use structured control flow statements like if
, for
, and while
to control the flow of your program.