I found that semi-colon are some time come to the end of the line. i.e.: end, else,... But just some time. Could you tell me when I need to put semi-colon to the line? And when I need not?

Recommended Answers

All 2 Replies

if (condition true)then Do something;
 {if you want to an 'else' part too then,do not put semi-colon}
if (condition true)then Do something  {do not here}
Else Do anything;{Here}
Because the semicolon at the end of 'if statement' is the sort version of 'If'

{or the Begin..end;}
{ if it is not the block of main begin..end then you have to put semicolon
}

Procedure Hello;
  Begin
      Write('Hello'); 
  End;   {see?}

Begin  {main}
   {call our procedure}
    Hello;
End.   {see?}
commented: His answer is very clear to me. +1

In Pascal, a semicolon separates statements (and terminates directives).

So the following is correct:

program Hello;  { Semicolon terminates directive }
begin
  writeln( 'Hello world!' )  { No semicolon necessary }
end.

As is:

program Hello;
begin
  write( 'Hello ' )
  ;  { semicolon separates statements }
  writeln( 'world!' )
end.

Since an if then..else block is one statement, there should not be any semicolons between the 'then' and 'else' parts:

if prime( users_number )
  then writeln( users_number, ' is prime.' )  { statement not ended }
  else writeln( users_number, ' is composite.' );  { now it is at the end }

Even nested ifs must obey:

if foo
  then if bar
         then baz
         else
  else quux;

Notice how if foo is true but bar is not, there is an empty statement there (or not there - depending on how you look at it ;) ).

This is the same in code like:

begin
  writeln( 'Woah, there are actually TWO statements here.' );
end.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.