else not attaching properly to if

Matti Niemenmaa see_signature at for.real.address
Mon Oct 29 08:07:01 PDT 2007


nobody wrote:
> I do not think it should ever be the case that this block:
> 
>   if(false)
>     if(true)
>     {
>       assert(false);
>     }
>   else
>   {
>     assert(true);
>   }
> 
> should ever be functionally different from this block:
> 
> 
>   if(false)
>   {
>     if(true)
>     {
>       assert(false);
>     }
>   }
>   else
>   {
>     assert(true);
>   }
> 
> The compiler agrees with me in this simple case.

No, you've got it wrong. It's:

if (false) {
	if (true) {
		assert (false);
	} else {
		assert (true);
	}
}

Which compiles down to nothing since if (false) is always false.

The compiler appears to agree because both blocks actually do nothing. Compile
and run this:

import std.stdio;

void main() {
	writefln("%d", f      (false,true));
	writefln("%d", f_wrong(false,true));
	writefln("%d", f_right(false,true));
}

int f(bool a, bool b) {
	if (a)
		if (b) {
			return 0;
		} else {
			return 1;
		}
	return 2;
}
int f_wrong(bool a, bool b) {
	if (a) {
		if (b) {
			return 0;
		}
	} else {
		return 1;
	}
	return 2;
}
int f_right(bool a, bool b) {
	if (a) {
		if (b) {
			return 0;
		} else {
			return 1;
		}
	}
	return 2;
}

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi


More information about the Digitalmars-d-bugs mailing list