Search code examples
pascalfreepascallazarus

Can't use break statement. Illegal expression error


In my for loop I used break statement to break loop after some match. But when I compile my code I got error for break:

Error: Illegal expression

Could you help me? I should add some unit?

for i:=0 to length(carsList)-1 do
        begin

             if numer <> carsList[i].numer then
             begin
                  tmpKw2 := carsList[i].rectangleRotate(carsList[i].angle);

                  if((polyLine(tmpKw2,linia.p1.x,linia.p1.y,linia.p2.x,linia.p2.y))) then
                  begin
                       kolizja := true;
                       break:=true;
                       maxV := carsList[i].v;
                       Break;
                  end;
             end;
        end; 

Solution

  • The code below works for me:

    program Testbreak;
    
    procedure TestBreakInFor;
    var
        i : Integer;
    begin
      for i := 0 to 10 do begin
        if Odd(i) then
          Break;
      end;
    end;
    
    begin
      TestBreakInFor;
      readln;
    end.
    

    The reason you are getting the "Illegal expression" error is that you are attempting to use Break as if it were a variable (Break := True) when it is not, it is an execution flow-control instruction, like Continue. Omit the := True and it should compile corrrectly.