Pascal error - ";" expected but Else found

20.7k views Asked by At

I am using pascal to do an assignment but keep running into this error '";" expected but Else found'. I have seen many questions asking this and tried to use them to help myself but no luck.

My code

Program TeamWrite;  
    Var FName, txt : String[10];  
    UserFile : Text;  
BEGIN          
    FName := 'Team';  
    Assign(UserFile, 'C:\Team.dat');  
    Rewrite(UserFile);  
    Writeln('Enter players name and score separated by a space, type end to finish');  
    if txt = 'end' then;  
        BEGIN  
            Close(UserFile)  
        End;  
    Else  
        BEGIN  
            Readln(txt);  
            Writeln;  
            Writeln(UserFile,txt);  
        End;  
    Until(txt = 'end');  

End.  
3

There are 3 answers

0
LairdPleng On

I'm not familiar with Pascal, but having a quick look through some websites I think you don't need the ; after the first if statement:

if txt = 'end' then;  

should probably be

if txt = 'end' then
3
ycsun On

In Pascal, semicolons (i.e. ";") are for separating statements, not ending them. So you should say:

if txt = 'end' then
  begin
    Close(UserFile)
  end
else
  begin
    Readln(txt);  
    Writeln;  
    Writeln(UserFile, txt)
  end

Note that there are no semicolons following then, preceding and following else, and following the two statements before end.

Also note that you can put a semicolon between a statement and an end, like:

begin
  WriteLn;
  WriteLn(txt);  <-- this is allowed
end

but the compiler would interpret it as if there is an empty statement following that semicolon:

begin
  WriteLn;
  WriteLn(txt);
  (an empty statement here)
end

which is harmless, though.

The "until" is an error, too, because it is a reserved word. In Pascal there is a "repeat...until" loop, like:

i := 0;
repeat
  WriteLn(i);
  i := i + 1
until i > 10

It's like C's "do...while" loop, only the condition is reversed. In your program I think you should have a repeat before the `if:

repeat
  if txt = 'end then
    ...
  else
    ...
until txt = 'end'
0
0yeoj On

You have misplaced some ;

if txt = 'end' then; // remove ';' 
    BEGIN  
        Close(UserFile) //add `;`
    End;  // remove ';' 
Else  
    BEGIN  
        Readln(txt);  
        Writeln;  
        Writeln(UserFile,txt);  
    End;  
Until(txt = 'end'); 

Please refer here