0

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?

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Duoas
1
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?}
Votes + Comments
His answer is very clear to me.
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.