Recent

Author Topic: missing semicolon  (Read 838 times)

cousinp

  • New Member
  • *
  • Posts: 25
missing semicolon
« on: September 24, 2024, 04:45:37 pm »
Code: Pascal  [Select][+][-]
  1.  
if (i >= 1) and (i<= 7) then
  begin
    if sw= 'Full' then
    writeln('The day in words is: ' + tmpStr)
    else
     writeln('The day in words is: ' + leftStr(tmpStr,3));
  end;
end;

The above code is from the book, part of a program
On the first 'writeln' there is no semicolon.
But the second 'writeln' has one, why not the first also.
I have tried no semicolons and the code does not produce an error and the program works.
I have also tried a semicolon on both statements and the code does not compile.
All previous 'writeln' statements in the book ended with a semicolon.
What makes this different?
Thanks,

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: missing semicolon
« Reply #1 on: September 24, 2024, 04:55:19 pm »
simply put, it should not be placed before else and may not be placed before end. if it is placed before end it is simply an empty statement:
Code: Pascal  [Select][+][-]
  1.   ...
  2.   end
  3.   ;
  4. end
  5. ;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18975
  • Glad to be alive.
Re: missing semicolon
« Reply #2 on: September 24, 2024, 05:02:18 pm »
if you put a ; semi column there the else will not be evaluated and it will not compile.
if else needs just a semi column after the else is fully evaluated.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Warfley

  • Hero Member
  • *****
  • Posts: 2050
Re: missing semicolon
« Reply #3 on: September 24, 2024, 05:03:24 pm »
; is not a statement terminator it's a statement separator. So you only need it to separate multiple statements.
The if-then-else statement is defined as
Code: XML  [Select][+][-]
  1. if <CONDITION> then <STATEMENT> else <Statement>
meaning between the then and the else only one statement is allowed. And because you can't have multiple statements, you can't have a semicolon. You have one in the end after the last statement, but this is not to terminate the else statement, but the if-then-else statement, because it is in a begin-end block that does allow for multiple statements delimited by semicolons.

This also means that you don't need any semicolons before an end:
Code: Pascal  [Select][+][-]
  1. begin
  2.   WriteLn('Hello World') // No semicolon here
  3. end;
Because you only have one statement, you don't need a statement seperator. So you could reduce your code to:
Code: Pascal  [Select][+][-]
  1. if (i >= 1) and (i<= 7) then
  2.   begin
  3.     if sw= 'Full' then
  4.       writeln('The day in words is: ' + tmpStr)
  5.     else
  6.      writeln('The day in words is: ' + leftStr(tmpStr,3))
  7.   end
  8. end;
« Last Edit: September 24, 2024, 05:26:47 pm by Warfley »

Paolo

  • Hero Member
  • *****
  • Posts: 695
Re: missing semicolon
« Reply #4 on: September 24, 2024, 09:02:08 pm »
Strictly speaking you can reduce further the code removing "begin-end" pair (even if the presence of begin-end makes the code more readable and less ambiguos).
Code: Pascal  [Select][+][-]
  1. if (i >= 1) and (i<= 7) then
  2.     if sw= 'Full' then
  3.       writeln('The day in words is: ' + tmpStr)
  4.     else
  5.       writeln('The day in words is: ' + leftStr(tmpStr,3));  //very likely here you need ";"
  6.  
  7.  
« Last Edit: September 24, 2024, 09:40:34 pm by Paolo »

cousinp

  • New Member
  • *
  • Posts: 25
Re: missing semicolon
« Reply #5 on: September 24, 2024, 09:59:08 pm »
Thank you everyone.
Paul

 

TinyPortal © 2005-2018