CSharp - Statement goto statement

Introduction

The goto statement transfers execution to another label within a statement block.

The goto statement has the following syntax:

goto statement-label;

when used within a switch statement:

goto case case-constant;    

You can only goto to constants branch in switch statement, not patterns.

A label is a marker for a code block.

You can add a label as follows:

startLoop:

The following code iterates the numbers 1 through 5. It uses if and goto to implement a loop,

Demo

using System;
class MainClass//  w w w.  ja v a  2 s .  co m
{
   public static void Main(string[] args)
   {
         int i = 1;
         startLoop:
         if (i <= 5)
         {
           Console.Write (i + " ");
           i++;
           goto startLoop;
         }
   }
}

Result