Goto
using System;
// The goto statement immediately transfers flow of control to the statement following a label.
public class GotoDemo
{
static void Main(String[] args)
{
int i = 0;
while (i < 5) // Executes until i<5
{
if (i == 3)
{
goto stop; // Control goes to the label 'stop' and thus exiting from while loop
}
Console.WriteLine("Hello world");
i++;
}
stop: // label
Console.WriteLine("Outside while loop");
Console.ReadLine();
}
}
Output:
Hello world
Hello world
Hello world
Outside while loop