tags:

views:

113

answers:

2

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?");
+5  A: 

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

Jonathan M Davis
I suppose I would have found it if I searched for errata and not addendum...:)
GMan
Yeah. Well, I knew where it was because I'm active on the D newsgroups. I would have had a much harder time finding it otherwise.
Jonathan M Davis
+2  A: 

You are correct. The example code is wrong. But the text in the book is correct: "If you instead want to bind the else to the first if, "buffer" the second if with a pair of braces". But the code doesn't show "buffering" just the second if.

Baxissimo