Labels and goto
Labels are names given to lines of code in order to direct program flow to those lines later on.
A label consists of a name and the :
character:
end: // ← a label
That label gives the name end to the line that it is defined on.
Note: In reality, a label can appear between statements on the same line to name the exact spot that it appears at, but this is not a common practice:
anExpression(); end: anotherExpression();
goto
goto
directs program flow to the specified label:
void foo(bool condition) { writeln("first"); if (condition) { goto end; } writeln("second"); end: writeln("third"); }
When condition
is true
, the program flow goes to label end
, effectively skipping the line that prints "second":
first third
goto
works the same way as in the C and C++ programming languages. Being notorious for making it hard to understand the intent and flow of code, goto
is discouraged even in those languages. Statements like if
, while
, for
etc. should be used instead.
For example, the previous code can be written without goto
in a more structured way:
void foo(bool condition) { writeln("first"); if (!condition) { writeln("second"); } writeln("third"); }
However, there are two acceptable uses of goto
in C, none of which is necessary in D.
Finalization area
One of the valid uses of goto
in C is going to the finalization area where the cleanup operations of a function are performed (e.g. giving resources back, undoing certain operations, etc.):
// --- C code ---
int foo() {
// ...
if (error) {
goto finally;
}
// ...
finally:
// ... cleanup operations ...
return error;
}
This use of goto
is not necessary in D because there are other ways of managing resources: the garbage collector, destructors, the catch
and finally
blocks, scope()
statements, etc.
Note: This use of goto
is not necessary in C++ either.
continue
and break
for outer loops
The other valid use of goto
in C is about outer loops.
Since continue
and break
affect only the inner loop, one way of continuing or breaking out of the outer loop is by goto
statements:
// --- C code --- while (condition) { while (otherCondition) { // affects the inner loop continue; // affects the inner loop break; // works like 'continue' for the outer loop goto continueOuter; // works like 'break' for the outer loop goto breakOuter; } continueOuter: ; } breakOuter:
The same technique can be used for outer switch
statements as well.
This use of goto
is not needed in D because D has loop labels, which we will see below.
Note: This use of goto
can be encountered in C++ as well.
The problem of skipping constructors
The constructor is called on an object exactly where that object is defined. This is mainly because the information that is needed to construct an object is usually not available until that point. Also, there is no need to construct an object if that object is not going to be used in the program at all.
When goto
skips a line that an object is constructed on, the program can be using an object that has not been prepared yet:
if (condition) { goto aLabel; // skips the constructor } auto s = S(42); // constructs the object properly aLabel: s.bar(); // BUG: 's' may not be ready for use
The compiler prevents this bug:
Error: goto skips declaration of variable deneme.main.s
Loop labels
Loops can have labels and goto
statements can refer to those labels:
outerLoop: while (condition) { while (otherCondition) { // affects the inner loop continue; // affects the inner loop break; // continues the outer loop continue outerLoop; // breaks the outer loop break outerLoop; } }
switch
statements can have labels as well. An inner break
statement can refer to an outer switch
to break out of the outer switch
statement.
goto
in case
sections
We have already seen the use of goto
in case
sections in the switch
and case
chapter:
-
goto case
causes the execution to continue to the nextcase
. -
goto default
causes the execution to continue to thedefault
section. goto case expression
causes the execution to continue to thecase
that matches that expression.
Summary
- Some of the uses of
goto
are not necessary in D. break
andcontinue
can specify labels to affect outer loops andswitch
statements.goto
insidecase
sections can make the program flow jump to othercase
anddefault
sections.