goto a no-go?

Ali Çehreli acehreli at yahoo.com
Tue Oct 1 10:15:34 PDT 2013


Nobody mentioned the use of goto with switch-case.

   http://ddili.org/ders/d.en/switch_case.html

goto can appear in three ways under case sections:

* 'goto case' causes the execution to continue to the next case.

* 'goto default' causes the execution to continue to the default section.

* 'goto expression' causes the execution to continue to the case that 
matches that expression.

The following program demonstrates these three uses by taking advantage 
of a foreach loop:

import std.stdio;

void main()
{
     foreach (value; [ 1, 2, 3, 10, 20 ]) {
         writefln("--- value: %s ---", value);

         switch (value) {

         case 1:
             writeln("case 1");
             goto case;

         case 2:
             writeln("case 2");
             goto case 10;

         case 3:
             writeln("case 3");
             goto default;

         case 10:
             writeln("case 10");
             break;

         default:
             writeln("default");
             break;
         }
     }
}

The output:

--- value: 1 ---
case 1
case 2
case 10
--- value: 2 ---
case 2
case 10
--- value: 3 ---
case 3
default
--- value: 10 ---
case 10
--- value: 20 ---
default

Ali



More information about the Digitalmars-d mailing list