In any other language it's simple, but in Pascal I still have problems. With integer variables, I want to read from user value between 1
and 12
, so I simply do:
Program HelloWorld(output);
{$mode objFPC}
var
a: integer;
begin
repeat
writeln('Provide a, a in [1,12]:');
readln(a);
until (a in [1..12]);
end.
What if I want to check, if a
is in [0,3.14159]
? So basically a
is real
not integer
. How to solve something like this?
The in
operator in pascal is not checking ranges but sets so
a in [1..12]
does not check if a
is in a range of 1..12 but checks whether a
is in a set constructed from the Integer
s from 1..12. This would be quite problematic for Real
values.
So the only consistent way would be checking
(a >= 0) and (a <= 3.14159)
Of course you can create a respective function if you want.
function inrange(val, lower, upper: real): boolean;
begin
inrange := (lower <= val) and (upper >= val);
end;
var a: real;
begin
repeat
writeln('Provide a, a in [0, 3.14159]:');
readln(a);
until inrange(a, 0, 3.14159);
end.