3

I am trying to figure out the correct way of passing an example that was using Free Pascal case statements to a simple if statement.

Using case would be

begin usingCaseStatements;

var
  user_age : Integer;

begin

  Writeln('Please enter your age');
  Readln(user_age);

  case user_age of
  1..12 : Writeln('You are too young');
  else
    Writeln('Invalid input');
  end;

  Writeln('Please any key to terminate the program');
  Readln();
end.

-----Using an if statement--------

begin usingCaseStatements;

var
  user_age : Integer;

begin

  Writeln('Please enter your age');
  Readln(user_age);

  if user_age in 1..12 then
    Writeln('You are too young')
  else
    Writeln('Invalid input');
  Writeln('Please any key to continue');
  Readln();
end.

I have tried replacing the "in" inside the if-statement snipet with no luck whatsoever at one point I tried doing if (user_age = 1..12) thenand it only gave me an error, the compiler states that the statement is waiting for ')' but that it found .. instead. I am extremely new to FPC so help and tips would be greatly appreciated.

Alex_adl04
  • 562
  • 6
  • 17

4 Answers4

6

IN tests for sets, not ranges. As TLama already commented, you can define a set containing the range using [1..12].

Most PC Pascals only support set sizes up to 256 elements though, so a solution as recommended by josifoski will be more practical long term.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • I had not gotten to sets yet, I do not think that they are covered at all in the tutorial I am using, I am trying to look for book alternatives and so far I have found one although it might be outdated. I will keep this way in mind, thank you Marco – Alex_adl04 Jul 29 '14 at 01:49
  • Search for "Essential Pascal" by Marco Cantu, if you don't already have it. – Marco van de Voort Jul 29 '14 at 15:14
3

if (user_age >=1) and (user_age <=12) then

Dan
  • 9,391
  • 5
  • 41
  • 73
josifoski
  • 1,696
  • 1
  • 14
  • 19
  • I figured I had to resort to something like that, I was looking for a more elegant alternative although I understand that I can't expect the same syntactical sugar that comes from other languages such as Python. Thank you josifoski – Alex_adl04 Jul 29 '14 at 01:48
  • The inRange function defined in the Math unit is one of the more elegant solutions that you may have in mind: https://www.freepascal.org/docs-html/rtl/math/inrange.html – jwdietrich Jun 24 '23 at 22:02
1

Just for fun. It works on FPC 2.7.1 but I do not know is it will works on the stable 2.6.4

program project1;

{$modeswitch typehelpers}

type
    TIntegerHelper = type helper for Integer
        function IsInRange(const ALow, AHigh: Integer): Boolean; inline;
    end;

    function TIntegerHelper.IsInRange(const ALow, AHigh: Integer): Boolean; inline;
    begin
        Result := (Self >= ALow) and (Self <= AHigh);
    end;
var
    i: Integer;

begin
    i := 8;
    Writeln(i.IsInRange(7, 9));
    Writeln(i.IsInRange(8, 8));
    Writeln(i.IsInRange(2, 3));
    Readln;
end.

Output:

TRUE
TRUE
FALSE
Abelisto
  • 14,826
  • 2
  • 33
  • 41
1

The statement if user_age in 1..12 then is very much Ada syntax. Free Pascal is fairly close: if user_age in [1..12] then

Here's a simple compilable example:

Program inRange;

var
  int1 : integer = 45;

begin
  if int1 in [4..200] then
    writeln('int1 is between 4 and 200')
  else
    writeln('int1 is not between 4 and 200')
end.

Output:

Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling scratch6.pas
Linking scratch6.exe
12 lines compiled, 0.1 sec, 28288 bytes code, 1332 bytes data

int1 is between 4 and 200
chikega
  • 135
  • 1
  • 6