Recent

Author Topic: [Solved] How to use numeric variables in case declaration?  (Read 1168 times)

loaded

  • Hero Member
  • *****
  • Posts: 825
[Solved] How to use numeric variables in case declaration?
« on: February 18, 2022, 08:51:53 am »
Hi All,
If we use the numeric value directly in the case statements, there is no problem, but if we use it over a numeric variable, an error occurs!

Sample code;
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.   var
  3.     i:integer=5;
  4.     ib:integer=4;
  5.   begin
  6.     case i of
  7.     0: begin
  8.         end;
  9.     end;
  10.  
  11.     case i of
  12.      ib: begin  // it gives error here!!!     [Error: Constant Expression expected]
  13.          end;
  14.      end;
  15.  end;
  16. end.

Why am I getting an error like this? Is there a different setting that needs to be made?
« Last Edit: February 18, 2022, 09:43:10 am by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Red_prig

  • Full Member
  • ***
  • Posts: 143
Re: How to use numeric variables in case declaration?
« Reply #1 on: February 18, 2022, 08:54:45 am »
operator case support only constant value

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to use numeric variables in case declaration?
« Reply #2 on: February 18, 2022, 09:19:51 am »
For testing variable values you have to use a sequence of if then tests:
Code: Pascal  [Select][+][-]
  1. if v = value1 then  Do1
  2. else if v = value2 then  Do2
  3. else if ...  etc.

440bx

  • Hero Member
  • *****
  • Posts: 4066
Re: How to use numeric variables in case declaration?
« Reply #3 on: February 18, 2022, 09:25:53 am »
As Red_prig stated, a case statement only supports comparisons against constants with the exception of strings.

However, there is a way to "synthesize" an inline case statement that works with variables by doing something like this:
Code: Pascal  [Select][+][-]
  1. for LoopVar := 1 to 1 do  { or "for i := i to i do" but only if "i" doesn't change in the "case" statement }
  2. begin
  3.   { first "case" }
  4.   if i = ib then
  5.   begin
  6.     { do whatever is appropriate when i = ib }
  7.  
  8.     break;
  9.   end;
  10.  
  11.   { second case }
  12.   if i = { some other variable } then
  13.   begin
  14.     { do whatever is appropriate in that case }
  15.  
  16.     break;
  17.   end;
  18.  
  19.   { and so on until all cases are accounted for }
  20. end;
  21.  
  22. { remaining function code follows the "for"/synthesized-case statement }

basically, the "for" loop is just a trick to create an inline scope to break out of.  The same thing can be done using a "repeat ... until TRUE".  Of course, the same thing can be accomplished by calling a function but, this way, the "case" is inline just as a normal "case" statement is.

Personally, I prefer this solution over nested "if"s which, as howardpc stated, is the more commonly used alternative.

HTH.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: How to use numeric variables in case declaration?
« Reply #4 on: February 18, 2022, 09:42:50 am »
Red_prig and howardpc and 440bx Thank you very much for taking your valuable time to reply.
Topic understood!!! We keep the if expression on defense.  :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Solved] How to use numeric variables in case declaration?
« Reply #5 on: February 18, 2022, 09:49:12 am »
Since in your original example you had assigned known constant values to your variables you could also do this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i:integer=5;
  4.   ib:integer=4;
  5.   iStr: String;
  6. begin
  7.   iStr := i.ToString;
  8.  
  9.   case iStr of
  10.     '0': begin
  11.          end;
  12.     '1': begin
  13.          end;
  14.     '4': DoIB;
  15.     '5': DoI;
  16.    otherwise      DoUnexpected;
  17.   end;
  18. end;

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: [Solved] How to use numeric variables in case declaration?
« Reply #6 on: February 18, 2022, 10:28:56 am »
howardpc,Sorry we got you a little tired, it's my fault. I didn't explain my problem well at first.
The process of assigning values to variables was for exemplary purposes.They are normally changed dynamically.
Still, a good example is the tostring expression in particular. Thank you very much
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018