Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-06

Is this if /else syntax wrong?

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

The following code generates a syntax error, if the semicolon after the if/else statement is removed.
Is its absence a syntax error? Pascal version is 2.2.4

procedure TfrmHometel.SetSplitterSizes;
var
 i: integer;
begin
  for i:= 0 to ComponentCount - 1 do
  begin
   if Components[i] is TSplitter then
   begin
     with TSplitter(Components[i]) do
     begin
      if Cursor = crHSplit then
        Width := 7
      else if Cursor = crVSplit then
        Height := 7;
      ; // <-- removing this semicolon results in a syntax error. Is it
syntax error some kind of extra strict compiler check.
      Color := clBlue;
     end;
   end;
  end;
end;
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Guillermo Martínez Jiménez
Show quoted text 21 lines
procedure TfrmHometel.SetSplitterSizes; var  i: integer; begin  for i:=3D 0 to ComponentCount - 1 do  begin   if Components[i] is TSplitter then   begin     with TSplitter(Components[i]) do     begin      if Cursor =3D crHSplit then        Width :=3D 7      else if Cursor =3D crVSplit then        Height :=3D 7;      ; // <-- removing this semicolon results in a syntax error. Is it syntax error some kind of extra strict compiler check.      Color :=3D clBlue;     end;   end;  end; end;

Compare with next:

      if Cursor =3D crHSplit then
        Width :=3D 7
      else
        if Cursor =3D crVSplit then
          Height :=3D 7; // <-- This semicolon is for the "if".
      ; // <-- This semicolon is for the "else".

Is like this:

IF ... THEN
BEGIN
  ...
END
ELSE BEGIN
  IF ... THEN
  BEGIN
    ...
  END;  // <-- This END is for the "IF".
END;  // <-- This END is for the "ELSE".

I recommend to use "BEGIN ... END" blocks in nested "IF" always to prevent this kind of mistakes.

Regards,
Guillermo "Ñuño" Martínez.

Reply by email

Frank Church
to Guillermo Martínez Jiménez

I will be upgrading to 2.4.0 soon.

It shouldn't be an error then.

2010/7/1 Guillermo Martínez Jiménez

Show quoted text 53 lines
> procedure TfrmHometel.SetSplitterSizes; > var > i: integer; > begin > for i:=3D 0 to ComponentCount - 1 do > begin > if Components[i] is TSplitter then > begin > with TSplitter(Components[i]) do > begin > if Cursor =3D crHSplit then > Width :=3D 7 > else if Cursor =3D crVSplit then > Height :=3D 7; > ; // <-- removing this semicolon results in a syntax error. Is it > syntax error some kind of extra strict compiler check. > Color :=3D clBlue; > end; > end; > end; > end; Compare with next: if Cursor =3D crHSplit then Width :=3D 7 else if Cursor =3D crVSplit then Height :=3D 7; // <-- This semicolon is for the "if". ; // <-- This semicolon is for the "else". Is like this: IF ... THEN BEGIN ... END ELSE BEGIN IF ... THEN BEGIN ... END; // <-- This END is for the "IF". END; // <-- This END is for the "ELSE". I recommend to use "BEGIN ... END" blocks in nested "IF" always to prevent this kind of mistakes. Regards, Guillermo "Ñuño" Martínez. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Mattias Gärtner
to Frank Church

Zitat von Frank Church :

Show quoted text 36 lines
I will be upgrading to 2.4.0 soon. It shouldn't be an error then. 2010/7/1 Guillermo Martínez Jiménez > > procedure TfrmHometel.SetSplitterSizes; > > var > > i: integer; > > begin > > for i:=3D 0 to ComponentCount - 1 do > > begin > > if Components[i] is TSplitter then > > begin > > with TSplitter(Components[i]) do > > begin > > if Cursor =3D crHSplit then > > Width :=3D 7 > > else if Cursor =3D crVSplit then > > Height :=3D 7; > > ; // <-- removing this semicolon results in a syntax error. Is it > > syntax error some kind of extra strict compiler check. > > Color :=3D clBlue; > > end; > > end; > > end; > > end; > > Compare with next: > > if Cursor =3D crHSplit then > Width :=3D 7 > else > if Cursor =3D crVSplit then > Height :=3D 7; // <-- This semicolon is for the "if". > ; // <-- This semicolon is for the "else".

No. A semicolon ends *all* open if/if-else/do statements. The second semicolon is an empty statement - a 'no operation'.

Show quoted text 15 lines
> Is like this: > > IF ... THEN > BEGIN > ... > END > ELSE BEGIN > IF ... THEN > BEGIN > ... > END; // <-- This END is for the "IF". > END; // <-- This END is for the "ELSE". > > I recommend to use "BEGIN ... END" blocks in nested "IF" always to > prevent this kind of mistakes.

In general I agree, but in this case there are no nested ifs, so it won't help here.

Mattias

Reply by email

Frank Church
to Mattias Gärtner

It is working now on 2.2.4. I think it was a temporary IDE glitch, which is what I suspected. Adding the ; made it go away, but it wasn't really the problem

2010/7/1 Mattias Gärtner

Show quoted text 74 lines
Zitat von Frank Church : I will be upgrading to 2.4.0 soon. > > It shouldn't be an error then. > > 2010/7/1 Guillermo Martínez Jiménez > > > procedure TfrmHometel.SetSplitterSizes; >> > var >> > i: integer; >> > begin >> > for i:=3D 0 to ComponentCount - 1 do >> > begin >> > if Components[i] is TSplitter then >> > begin >> > with TSplitter(Components[i]) do >> > begin >> > if Cursor =3D crHSplit then >> > Width :=3D 7 >> > else if Cursor =3D crVSplit then >> > Height :=3D 7; >> > ; // <-- removing this semicolon results in a syntax error. Is it >> > syntax error some kind of extra strict compiler check. >> > Color :=3D clBlue; >> > end; >> > end; >> > end; >> > end; >> >> Compare with next: >> >> if Cursor =3D crHSplit then >> Width :=3D 7 >> else >> if Cursor =3D crVSplit then >> Height :=3D 7; // <-- This semicolon is for the "if". >> ; // <-- This semicolon is for the "else". >> > No. A semicolon ends *all* open if/if-else/do statements. The second semicolon is an empty statement - a 'no operation'. Is like this: >> >> IF ... THEN >> BEGIN >> ... >> END >> ELSE BEGIN >> IF ... THEN >> BEGIN >> ... >> END; // <-- This END is for the "IF". >> END; // <-- This END is for the "ELSE". >> >> I recommend to use "BEGIN ... END" blocks in nested "IF" always to >> prevent this kind of mistakes. >> > In general I agree, but in this case there are no nested ifs, so it won't help here. Mattias _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Jonas Maebe
to Frank Church

Frank Church wrote on Thu, 01 Jul 2010:

It is working now on 2.2.4. I think it was a temporary IDE glitch, which is what I suspected. Adding the ; made it go away, but it wasn't really the problem

If you are using the textmode IDE, you might want to wait with updating until 2.4.2, because there was a bug in the 2.4.0 IDE that, in many cases, causes compilation to always fail (until a restart of the IDE) after an error occurred once (even if you fix that error).

Jonas

---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Reply by email