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 ;)
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With