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

user1000441 picture user1000441 · Oct 18, 2011 · Viewed 13.2k times · Source

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?

Answer

da-soft picture da-soft · Oct 18, 2011

If Number has some integer data type, then:

case number of
5 .. 10: lblAnswer.Caption := 'in range';
else     lblAnswer.Caption := 'out of range';
end;