Syntax Error; expected but else found (Pascal)

1

I'm trying to do nests of if in Pascal, but in all the programs that I try to do I get the same error

  

Syntax Error; expected but else found

For example

program temperatura;

const

    t=35;
    Hipotermia: real=36;
    Fiebre: real=37.5;
    Hiperpirexia: real=41;
begin

    if (t<=Hipotermia)then
        writeln('El paciente tiene hipotermia');
    else 
        if (t>Hipotermia) and (t<=Fiebre)then
            writeln('El paciente tiene una temperatura normal');
        else
            writeln('El paciente tiene fiebre');
end.
    
asked by Raul 10.10.2018 в 16:07
source

2 answers

1

Some theory:

In pascal

  • all sentences are terminated by ; . This is the symbol that tells the compiler that it ends a complete statement.
  • the if then and the if then else are different statements . Each one ends with a ; .
  • when you have several statements, the%% share% / begin the group in a block.

In practical terms, the following are valid passcal sentences.

if x = 1 then
  x := 2;  //acá termina la sentencia if then

//en este caso, para que varias sentencias formen parte del then, 
//las encerramos en begin/end
if x = 1 then
begin
  x := 2;
  y := 3;
end;

if x = 1 then
  x := 2 
else
  x := 3; //hasta acá termina una sentencia if then else completa, no hay ; antes

if x = 1 then
begin
  x := 2;
  y := 3;
end
else
begin
  x := 3; 
  y := 4;
end; 

And this sentence, will give error:

if x = 1 then
  x := 2; //este ; termina una sentencia if then completa
else      //este else sería una sentencia nueva, es un error
  x := 3; 
    
answered by 11.10.2018 в 22:48
0

Remember that you must also put the begin and the end within the if, like this:

   program temperatura;

    const t=35;

    Hipotermia: real=36;
    Fiebre: real=37.5;
    Hiperpirexia: real=41;

    begin
        if (t<=Hipotermia)then
            begin
                 writeln('El paciente tiene hipotermia');
            end
        else 
            begin
                if (t>Hipotermia) and (t<=Fiebre)then
                    begin
                        writeln('El paciente tiene una temperatura normal');
                    end
                else
                    begin
                        writeln('El paciente tiene fiebre');
                    end
            end
    end.

I have also seen that some put it; At the end of the last end of the if, if it does not work, try putting them in the code above.

Greetings and I hope you serve!

    
answered by 10.10.2018 в 16:30