Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi fall through in case statement

Tags:

delphi

In C you can do something like

switch(x) {
  case 'a':
  case 'b': 
    printf("something");
  break;
  case 'c': 
    printf("else");
  break;
}

while in Delphi I tried both

 case x of
   'a': 
   'b': writeln('something');
   'c': writeln('else');
 end;

and

 case x of
   ['a','b']: writeln('something');
   'c': writeln('else');
 end;

but both of them do not work.

I though of different solutions, e.g. writing a procedure and call it both for 'a' and for 'b', but I was wondering if there was a better solution. I could also use a goto, like this:

 case x of
   'a': goto labelCaseB;
   'b': begin
          labelCaseB:
          writeln('something');
        end;
   'c': writeln('else');
 end;

and it works perfectly, but what is the "standard" solution for the fall through in the case statement in Delphi language?

Of course, my actual case is far more complicated: in the example, I would have used an if-else ;)

like image 853
ZioBit Avatar asked Jul 21 '16 10:07

ZioBit


1 Answers

Delphi does not have fall through in case statements. It is one of the major differences between C and Delphi. But in your particular case (sorry about the pun) you can write

 case x of
   'a','b': begin
          writeln('something');
        end;
   'c': writeln('else');
 end;
like image 128
Dsm Avatar answered Nov 08 '22 20:11

Dsm