Search code examples
d

Problem with Book Example


Is it just me, or is there a problem with page 68 of "The D Programming Language" ? On this page, the author discusses D's syntax of if-else statements and how they nest. He first presents this example:

if(a == b)
    if(b == c)
        writeln("all are equal!");
    else
        writeln("a is different from b. Or is that so?");

He then points out that the else will bind to the second if. He then says that, to get the else to bind to the first if, one should use braces like so:

if(a == b) {
    if(b == c)
        writeln("all are equal!");
    else
        writeln("a is different from b. Or is that so?");
}

Am I missing the point completely, or would you have to do this:

if(a == b) {
    if(b == c)
        writeln("all are equal!");
}
else
    writeln("a is different from b. Or is that so?");

Solution

  • It is indeed an error. The errata for TDPL can be found here: http://www.erdani.com/tdpl/errata/index.php?title=Main_Page