# Board index » delphi » using an REAL as part of a CASE statement

## using an REAL as part of a CASE statement

Hi All,  I would appreciate some help on what must be a simple problem, but
I've been unable to crack it so far.

I want to use the integer part of a real variable as the index within a CASE
statement.  I can use the INT function to get the integer component of the
REAL variable, but can I convert this into an Integer variable to use in the
CASE statement.

Any help would be welcome.

Thanks, Mike

## Re:using an REAL as part of a CASE statement

##### Quote
"mike.hastings" wrote:

> Hi All,  I would appreciate some help on what must be a simple problem, but
> I've been unable to crack it so far.

> I want to use the integer part of a real variable as the index within a CASE
> statement.  I can use the INT function to get the integer component of the
> REAL variable, but can I convert this into an Integer variable to use in the
> CASE statement.

The problem is that int() returns a real. Reals are not allowes in case
statements. You need to convert real to integer. This can be done using
round() (which rounds) or trunc() (which cuts off anything after the

case round(real_var) of
1: ...
2: ...
end

Wolf

## Re:using an REAL as part of a CASE statement

JRS:  In article <99ar4r\$ls...@plutonium.btinternet.com>, seen in
news:comp.lang.pascal.borland, mike.hastings <micheal.nospam.hastings@bt
internet.com> wrote at Wed, 21 Mar 2001 18:17:46 :-

##### Quote
>I want to use the integer part of a real variable as the index within a CASE
>statement.  I can use the INT function to get the integer component of the
>REAL variable, but can I convert this into an Integer variable to use in the
>CASE statement.

Don't use Int, which gives a float; use Trunc or Round, which give
longints.  See the on-line help, and maybe my pas-real.htm.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

## Re:using an REAL as part of a CASE statement

Use ROUND as well:

var
r : real;
i1, i2 : integer;

begin
r := 0;
for i1 := 1 to 10 do
begin
r := r + 0.4;
i2 := round(int(r));
case i2 of
0 : writeln('0');
1 : writeln('1');
2 : writeln('2');
3 : writeln('3');
4 : writeln('4');
5 : writeln('5');
6 : writeln('6');
7 : writeln('7');
end;
end;
end.

--
Groetjes, Ruud

http://Ruud.C64.org/

## Re:using an REAL as part of a CASE statement

Thanks Dr. John
will try it out.

regards, Mike

"Dr John Stockton" <s...@merlyn.demon.co.uk> wrote in message
news:C6tcClDlLTu6EwyA@merlyn.demon.co.uk...

##### Quote
> JRS:  In article <99ar4r\$ls...@plutonium.btinternet.com>, seen in
> news:comp.lang.pascal.borland, mike.hastings <micheal.nospam.hastings@bt
> internet.com> wrote at Wed, 21 Mar 2001 18:17:46 :-

> >I want to use the integer part of a real variable as the index within a
CASE
> >statement.  I can use the INT function to get the integer component of
the
> >REAL variable, but can I convert this into an Integer variable to use in
the
> >CASE statement.

> Don't use Int, which gives a float; use Trunc or Round, which give
> longints.  See the on-line help, and maybe my pas-real.htm.

> --
> ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00
MIME. ?
>  Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, &