Recent

Author Topic: CASE statement question  (Read 7152 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
CASE statement question
« on: July 19, 2011, 10:30:41 pm »
RealX being a real, can't seem to make this work:

Case RealX of
<> 1 :  IsNotOne;
 =  1 :  IsOne,;
  <  1 : LessThanOne ;
 >  1 :  IllegalOperation;
 end;


IsNotOne, IsOne, etc. are the code to do as per value of RealX. Looks like this is not allowed?

If so, how?

Thanks!
   

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: CASE statement question
« Reply #1 on: July 19, 2011, 10:35:00 pm »
Case only works with ordinal types.
http://www.freepascal.org/docs-html/ref/refsu47.html
Contrary to what is mentioned in the manual, it does work with booleans (which is an ordinal type).
« Last Edit: July 19, 2011, 10:46:29 pm by eny »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: CASE statement question
« Reply #2 on: July 19, 2011, 10:56:47 pm »
Thanks Eny...

I'll do it with if statements then, unless some other way is pointed out to.


IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: CASE statement question
« Reply #3 on: July 19, 2011, 11:18:17 pm »
I occasionally wish there was a "caseSTR <string> of" construct.
It would make my code easier to read rather than having a bunch of if then/else statements.

I did run across this 2002 conversation on the fpc list:
http://lists.freepascal.org/lists/fpc-pascal/2002-February/002799.html

Quote
Case with strings is not supported, and probably will never be
supported. One way of doing the above is to

Code: [Select]
const
   N = 3;
   names : Array [1..N] string [10] = ('abcdeefdre','gfsgeyeyey','jfmvnvyret');

Var
  I,Which : Integer;

begin
  I:=1;
  Which:=0;
  While (Which=0) and (I<=N) do
    If Names[I]=ParamStr(1) then
      Which:=I
    else
      Inc(I);
  Case Which of
   1 : ;
   2 : ;
   3 : ;
  else
    // Zero, invalid option.
  end

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: CASE statement question
« Reply #4 on: July 19, 2011, 11:26:36 pm »
@Elmug: If you need a simple comparisson you can do the following:
Code: Pascal  [Select][+][-]
  1. { CompareValue is in Math unit }
  2. i := 1;
  3. case CompareValue(RealX, i) of
  4.    -1: { RealX is Minor than i } LessThanOne;
  5.    0: { RealX is Equal to i } IsOne;
  6.    1: { RealX is Bigger than i } IllegalOperation;
  7. end;
  8.  

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: CASE statement question
« Reply #5 on: July 19, 2011, 11:32:22 pm »
@ IPguy
Quote
I occasionally wish there was a "caseSTR <string> of" construct.
It would make my code easier to read rather than having a bunch of if then/else statements.

I did run across this 2002 conversation on the fpc list:
http://lists.freepascal.org/lists/fpc-pascal/2002-February/002799.html

But it is from 2002 !  :o

Times changed, we have 2011.

This already works, at least one year ! (It didn't work in fpc 2.4.2 but it was available in SVN).
Code: [Select]
var str: string;
begin
  case str of
  'january': ;
  'february': ;
  'march': ;
  end; 
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: CASE statement question
« Reply #6 on: July 19, 2011, 11:39:47 pm »
Good news !!  Blaazen - Thanks for the update.

... and more work to do - now to rip out working code and implement this.
However, the readability gains are worth the short term effort.


Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: CASE statement question
« Reply #7 on: July 19, 2011, 11:42:05 pm »
Great tips. I can put the comparevalue to use right away, and the case string is also very useful.

Big Thanks !

 

TinyPortal © 2005-2018