3

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.

riki481
  • 606
  • 1
  • 5
  • 12

1 Answers1

5

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Now I get it, similar from setting the size of an array. Thanks for the help – riki481 Jul 29 '20 at 18:44
  • 1
    Actually, you don't need to use a special case for the `else` scenario. You can use `19..idade.MaxValue: message := 'third option'` in modern versions of Delphi, and `19..MaxInt: message := 'third option'` in Delphi 6 (assuming `idade` is of type `Integer`). – Andreas Rejbrand Jul 29 '20 at 18:49
  • I usually go to delphi basics website to see documentation for delphi, I'm going to start using embarcadero as well, thanks for the tip. About the `(number >9)`, I actually forgot to put the `=` to verify if the number is either 8 or 10 as well. – riki481 Jul 29 '20 at 18:51
  • 1
    @riki481 DelphiBasics is very old, and lacks docs for many overloads and modern compiler features. On the other hand, the search engine on Embarcarero's DocWiki is not very good, so more times than not I end up using Google to find things in the DocWiki by restricting searches to just the DocWiki site. – Remy Lebeau Jul 29 '20 at 18:53
  • 1
    @riki481 It is not similar to setting size of arrays. When using case statements you actually provide each branch a set of values which will trigger that branch of case statement code. And for defining sets you can use `12..16` format which defines a set of all numbers from 12 to 16 including both 12 and 16. You can use `1,3,8` to include only numbers 1, 3 and 8 in a set. Or you may also use both approaches combined like `1,3..5,7` which would define a set containing numbers 1, 7 and all numbers between 3 and five including 3 and five.... – SilverWarior Jul 29 '20 at 19:46
  • 1
    ... I recommend you read more about sets in [Delphi documentation](http://docwiki.embarcadero.com/RADStudio/Sydney/en/Structured_Types_(Delphi)#Sets) in order to help you learn how to get most out of case statements since in most cases you would want to use case statement in order to execute certain code for certain set of conditions – SilverWarior Jul 29 '20 at 19:49