0
votes

I have a problem with my very simple Pascal code. (I just started to learn Pascal.)

So it's about an age comparison code then rest can be seen through the code.

program Test;
uses crt;
var
  age : real;
Begin

writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.

When I enter a value below 18 as the age, I expect the program to respond:

ACCESS DENIED
Reason:You are way to young

but I don't get any output. Why?

1
what is the problem ? What result do you get, and what did you expect ? - Aserre
Yeah sorry i forgot to mention that. When you enter an age under 18 the text wont appear i expected that it will show that part"writeln('ACCESS DENIED'); writeln('Reason:You are way to young');" - Pain
have you tried writing if age>18 AND age<100 instead of your first 2 if ? Your code seems to miss at least one Begin as of now, and maybe an end - Aserre
I tried " if age>18 AND age<100 " and Ive got these errors from the compiler "test.pas(9,11) Error: Operation "and" not supported for types "Real" and "Real" " btw im using Lazarus - Pain
Some indention, including lining up your if/else/end keywords so that you can readily see the structure of your program might help... In particular, it seems you have if age<18 nested inside if age>18, so it will never be true... - twalberg

1 Answers

3
votes

Sometimes text indentation helps you to see the issue. Here's your code with indentation added:

program Test;
uses crt;
var
  age : real;
Begin
  writeln('Enter your age: ');
  readln(age);
  if age>18 then
    if age<100 then
    Begin
      clrscr;
      textcolor(lightgreen);
      writeln('Access Granted');
    end
    else
      if age<18 then
      Begin
        clrscr;
        textcolor(lightred);
        writeln('ACCESS DENIED');
        writeln('Reason:You are way to young');
      end
      else
      Begin
        clrscr;
        textcolor(lightred);
        writeln('ACCESS DENIED');
        writeln('Reason:You are way to old');
      end;
  readln;
end.

And to make the implemented logic more obvious, I will now represent the nested ifs without the code that they execute:

  if age>18 then
    if age<100 then
      ...  // Access Granted
    else
      if age<18 then
        ...  // You are way too young
      else
        ...  // You are way too old
  ;

It is easy to see now that the branch marked as You are way too young is never reached. It is supposed to be executed when age is less than 18, but that if statement is nested into another if which will call it only when age is greater than 18. So, age should first qualify as greater than 18, then less than 18 in order for that branch to execute – you can see now why you do not get the expected result!

The intended logic could possibly be implemented this way:

  if age>18 then

    if age<100 then
      ...  // Access Granted
    else  // i.e. "if age >= 100"
      ...  // You are way too old

  else  // now this "else" belongs to the first "if"

    ...  // You are way too young

  ;

I believe you should be able to fill in the missing code blocks correctly.

Just one last note: you might want to change age>18 to age>=18, so that 18 proper does not qualify as "too young".