Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test that a value is within a range with a "case" statement instead of an "if" statement?

Can the following if statement be converted to a case statement?

if (Number >= 5) and (Number <= 10) then
  lblAnswer.Caption := 'in range'
else
  lblAnswer.Caption := 'out of range';

My Answer :

Yes it can

case (number >= 5) and (Number <= 10) of
  lblAnswer.Caption := 'in range';
  lblAnswer.Caption := 'out of range';
end;

Is this correct?

like image 205
user1000441 Avatar asked Oct 18 '11 05:10

user1000441


1 Answers

If Number has some integer data type, then:

case number of
5 .. 10: lblAnswer.Caption := 'in range';
else     lblAnswer.Caption := 'out of range';
end;
like image 86
da-soft Avatar answered Sep 22 '22 06:09

da-soft