(13,3)致命:语法错误,“;”预期,但找到“ ELSE”

问题描述 投票:0回答:1
Program prueba1;
uses Estructu;
Var Pila1:Pila; Fila1,Completa:Fila;
Begin
    Inicfila (Completa);
    readpila(Pila1);
    readfila(Fila1);
    While (not pilavacia(Pila1) and not filavacia(Fila1)) do
    begin
        if (tope(Pila1) > primero(Fila1)) then
        begin
            agregar(Completa, desapilar(Pila1))
        else
            if (tope(Pila1) < primero(Fila1)) then
            begin
                agregar(Completa, extraer(Fila1))
            else
                if (tope(Pila1) = primero(Fila1)) then
                begin
                    agregar(Completa, desapilar(Pila1));
                    agregar(Completa, extraer(Fila1))
                end
            end
        end
    end
    write('El resultado final de Completa es');
    Writefila(Completa);
End.

该程序的目的是在Completa中按从头到尾的顺序组织来自Pila1和Fila1的所有变量。我不知道自己在做什么错,希望能得到帮助

freepascal
1个回答
0
投票

您没有正确使用if ... then ... elsebegin ... end

具有block的概念,该概念以begin开头并以end结束。任何需要单个statement的地方,都可以放一个blockif <condition> then <statement> else <statement>也是这种情况。

因此,此代码有效:

if something() then
  stuff
else
  stuff;

...是这样:

if something() then
begin
  stuff;
  moreStuff;
end
else
begin
  otherStuff;
  moreOtherStuff;
end;

但是,(您正在使用的)不是:

if something() then
begin
  stuff // I guess here you omitted the semicolon because you correctly remembered
        // that there shouldn't be a semicolon before `else`, but...
else // WRONG, this is in the middle of the block!
  otherStuff;
end;

[为什么,让我们修复缩进以匹配此代码的逻辑解释:

if something() then
begin
  stuff
  else // ????????
  otherStuff;
end;

由于begin ... else ... end不是有效的构造,因此会出现错误。由于在begin之前有一个end,但是没有else,因此您的else位于then块的中间,这没有任何意义。

开始end部分之前,请确保先else您的块,然后再begin创建一个新块。

© www.soinside.com 2019 - 2024. All rights reserved.