Recent

Author Topic: Empty THEN statement gives no error  (Read 2438 times)

ButtonSystem

  • Newbie
  • Posts: 4
Empty THEN statement gives no error
« on: August 30, 2014, 08:58:20 pm »
The Free Pascal Compiler does not give an error when compiling an if-then statement that has no "then" statement. For example:

Code: [Select]
program project1;

procedure AnyProc;
  var
    Int1,Int2:integer;
  begin
  Int1:=0;
  Int2:=0;
  if Int1=Int2 then
  end;

begin
AnyProc;
end.

This program compiles and runs without an error. Is this a bug?

It works whether or not the "then" is followed by a semicolon.

Windows 7, FPC 2.6.4 [2014/03/06] for i386

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Empty THEN statement gives no error
« Reply #1 on: August 30, 2014, 09:11:06 pm »
Is this a bug?
No.

It works whether or not the "then" is followed by a semicolon.
Last statement in a block does not need a semicolon.

Code: [Select]
var
 a: integer;
..
begin
  a := 1
end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Empty THEN statement gives no error
« Reply #2 on: August 30, 2014, 09:34:04 pm »
Perhaps it would help you to think of an empty "then ;" statement as containing an implied "nothing"
Code: [Select]
if someBooleanCondition
then
  // do nothing
;
For instance:
Code: [Select]
function WritelnIfEven(anInt: integer): boolean;
begin
  if Odd(anInt) then
  else
    writeln('the value ',i,' is even');
  result:=not Odd(anInt);
end;
« Last Edit: August 30, 2014, 09:35:36 pm by howardpc »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Empty THEN statement gives no error
« Reply #3 on: August 30, 2014, 10:06:08 pm »
Or it would help to think of semicolons to separate statements between the begin and end. So adding a semicolon before end would insert a dummy/empty statement.

You can read about it here:
Quote
Pascal programs start with the program keyword with a list of external file descriptors as parameters[14] (not required in Turbo Pascal etc.); then follows the main block bracketed by the begin and end keywords. Semicolons separate statements, and the full stop (i.e., a period) ends the whole program (or unit). Letter case is ignored in Pascal source.
(source)

And in the early versions of Pascal it was not even permitted to do a semicolon before end.
Quote
The presence of an extra semicolon was not permitted in early versions of Pascal.
(source)

But nowadays:
Quote
Programmers usually include these extra semicolons out of habit, and to avoid changing the last line of a statement sequence when new code is appended.

ButtonSystem

  • Newbie
  • Posts: 4
Re: Empty THEN statement gives no error
« Reply #4 on: August 31, 2014, 03:25:22 pm »
Thank you. I wasn't aware that this was valid Pascal syntax, as I always have something for the "then" statement.

 

TinyPortal © 2005-2018