I'm using delphi to run the following code:
if (number> 8) and (number< 10) then
message:= 'first option'
else if (number> 11) and (number< 17) then
message:= 'second option'
else if (number> 18) then
message:= 'third option';
I need to do the exact code but using case
, I am trying but didn't find any content that explains how to do it:
case idade of
(case > 8 and case< 10) : message:= 'first option';
(case > 11 and case< 17) : message:= 'second option';
(case > 18) : message:= 'third option';
end;
I've tried to search for questions about case as well, but I guess I didn't find the correct way to find for this answer as well.
The closest you can get to that result using a case
statement would look like this:
case idade of
9: message := 'first option';
12..16: message := 'second option';
else
if idade > 18 then
message := 'third option';
end;
Or this (thanks @AndreasRejbrand):
case idade of
9: message := 'first option';
12..16: message := 'second option';
19..MaxInt{idade.MaxValue}: message := 'third option';
end;
You might want to read Embarcadero's documentation on how Case Statements actually work.
Do note that in the original code, if (number> 8) and (number< 10) then
is the same as if (number = 9) then
, and you are skipping the assignment of message
if number
is either 10, 17, or 18, is that what you really want?