SRL Archive - Help and Support

Case Statement question


Footy

2012-11-01 17:50:34

Say I have a case statement, and I want something to happen if the variable inv is one of many un-consecutive numbers. Can I make this one line, or do I have to make many lines like below?

case inv of
1: Something;
5: Something;
9: something;
13: something;
18: something;
21:something;
25: something;
end;

I tried or like below, but it didn't work. It compiled, but didn't write the line. Help would be appreciated! :)

program new;
{$i srl/srl.simba}
const
ran = 20;

begin
SetupSRL
case ran of
10 or 20: writeln('Yay');
end;
end.

J J

2012-11-01 17:55:40

I would go with one of these.

procedure Test;
var
i, j: Integer;
GoodOptions: TIntegerArray;

begin
i := 18;
case i of
1, 5, 9, 13, 18, 21: writeln('correct');
end;

GoodOptions := [1, 5, 9, 13, 18, 21];
for j:=0 to high(GoodOptions) do
if (i = GoodOptions[j]) then
begin
writeln('correct');
break;
end;
end;

Nebula

2012-11-01 17:55:47

program new;
{$i srl/srl.simba}
const
ran = 20;

begin
SetupSRL
case ran of
10, 20: writeln('Yay');
end;
end.

Footy

2012-11-01 17:56:56

Ah, thanks guys, I didn't know you could use the comma. Repped both, thanks!

riwu

2012-11-01 20:13:06

Use an Arithmetic progression:
for r:=1 to 6 do
begin
SetLength(iArr, iArr + 1);
iArr[High(iArr)]:= (1 + (r - 1) * 4);
end;