Switch and break

Era Scarecrow rtcvb32 at yahoo.com
Thu Jan 19 16:38:49 PST 2012


== Quote from Derek (ddparnell at bigpond.com)'s article
> I use a language that enables the coder to choose to use fall through or
> not. By default, falling through is disabled, so to produce the D effect
> one can code ...
>   	switch i do
>   		case 1 then
>   		writeln("You wrote 1")
>   	 fallthru
>   		case 2 then
>   		writeln("You wrote 2")
>   	 fallthru
>   		case 3 then
>   		writeln("You wrote 3")
>   	 fallthru
>   		else case then
>   		writeln("I said: 1 or 2 or 3!")
>          end switch

 Personal experience in a program (not a language) is that having certain options
on automatically usually is more of an annoyance, than manually turning them on.
With that said, having explicit fall through sounds more useful than explicit
breaking. However people coming form C and C++, the switch statement changes would
be enough to cause their own bugs and perhaps discouragement.

 The true use in fallthrough, would be if you wanted more than one option to
trigger a specific block of code. An example could be with flags, Unix flags have
somewhat of a standard, so a program that defined it's own flags may later add
secondary flags for compatibility purposes.

switch(i) {
  case 2:
  case 3:
  case 5:
  case 7:
    printf("%d is a prime between 1 and 10", i);
    break;
  default:
    printf("%d is not a prime between 1 and 10", i);
}


More information about the Digitalmars-d-learn mailing list