The goto statement - CSharp Language Basics

CSharp examples for Language Basics:goto

Introduction

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

The form is as follows:

goto statement-label;

Or, when used within a switch statement:

goto case case-constant;

A label is a placeholder in a code block that precedes a statement, denoted with a colon suffix.

The following iterates the numbers 1 through 5, mimicking a for loop:

Demo Code

using System;/*  w  w w. ja  v  a  2s  .  c o m*/
class Test
{
   static void Main(){
      int i = 1;
      startLoop:
      if (i <= 5)
      {
         Console.Write (i + " ");
         i++;
         goto startLoop;
      }
   }
}

Result


Related Tutorials