Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

goto

Part of KeywordsCategory

Description

D supports goto (but you're not required to use it).

Example

/*
File:       goto.d
Date:       2004-02-16
Author:     by J C Calvarese, http://jcc_7.tripod.com/d/
License:    Public Domain
Purpose:    Shows the usage of goto and labels.
Edited:     2006-01-23, Christopher Nicholson-Sauls

Why goto is part of the D programming language...
* goto is part of C, so it can facilitate porting of C/C++ code.
* goto is easy to implement.

Why you should avoid using goto...
* If you use too many goto's you can run into the spaghetti code syndrome.
* Using goto can make the code more difficult to understand for yourself and other.
* Usually, there's an easy way to do it without goto.

In the end, it's each programmer's choice, but I think that goto use should be minimized.

*/

import std.stdio;

void main () {

thebeginning:
    writefln("The beginning");

part1:
    writefln("Part 1");

part2:
    writefln("Part 2");
    goto part5;

part3:
    writefln("Part 3");

part4:
    writefln("Part 4");

part5:
    writefln("Part 5");

theend:
    writefln("The end");
}

Output

The beginning
Part 1
Part 2
Part 5
The end