Recent

Author Topic: if then else syntax  (Read 1613 times)

waltfair

  • New Member
  • *
  • Posts: 21
  • Walt Fair, PHD, PE. Engineer and software junkie
if then else syntax
« on: June 01, 2023, 11:47:50 pm »

What is wrong with this snippet of code?
The compiler complains "; expected but ELSE found."
Code: Pascal  [Select][+][-]
  1.  if(i>= Length(s))then t:=s;
  2.   else
  3. begin
  4. t:= Copy(s,1,i);
  5. s:= Copy(s,i+1,Length(s)-i);
  6. end;
  7. Firstword:= t;
  8. end;
  9.  

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: if then else syntax
« Reply #1 on: June 01, 2023, 11:49:40 pm »

What is wrong with this snippet of code?
The compiler complains "; expected but ELSE found."
Code: Pascal  [Select][+][-]
  1.  if(i>= Length(s))then t:=s;
  2.   else
  3. begin
  4. t:= Copy(s,1,i);
  5. s:= Copy(s,i+1,Length(s)-i);
  6. end;
  7. Firstword:= t;
  8. end;
  9.  

Code: Pascal  [Select][+][-]
  1.  if(i>= Length(s))then t:=s//;<--- this semicolon is wrong.
  2.   else
  3. begin
  4. t:= Copy(s,1,i);
  5. s:= Copy(s,i+1,Length(s)-i);
  6. end;
  7. Firstword:= t;
  8. end;
  9.  

Kays

  • Hero Member
  • *****
  • Posts: 613
  • Whasup!?
    • KaiBurghardt.de
Re: if then else syntax
« Reply #2 on: June 02, 2023, 10:45:08 pm »
[In addition to eljo’s reply, some explanation:] Remember that in Pascal the semicolon is a statement separator, not a terminator like in many other programming languages.The if statement is composed of
Note, it says statement (singular), not statements (plural). Thus you have t:=s as one statement. A second statement (properly separated by ;) as part of the then branch is not allowed according to the syntax repeated above.
Yours Sincerely
Kai Burghardt

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: if then else syntax
« Reply #3 on: June 03, 2023, 01:34:53 pm »
this compile, even if it looks strange

Code: Pascal  [Select][+][-]
  1.   repeat
  2.     if condition1 then
  3.   until condition2;
  4.  

no statement after then (or better hidden no-statement without ";").

440bx

  • Hero Member
  • *****
  • Posts: 4742
Re: if then else syntax
« Reply #4 on: June 03, 2023, 02:09:04 pm »
As @Kays pointed out, semicolon in Pascal is a statement separator (with one exception in Delphi and FPC), this means that "else" cannot be preceded by a semicolon because "else" is not a statement.

this compile, even if it looks strange

Code: Pascal  [Select][+][-]
  1.   repeat
  2.     if condition1 then
  3.   until condition2;
  4.  

no statement after then (or better hidden no-statement without ";").
the "repeat until" pair also includes the compound statement functionality (that's why it does not require a "begin/end" pair to apply to multiple statements.)  Because of this, the following also compiles:

Code: Pascal  [Select][+][-]
  1.   repeat
  2.     if condition1 then ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
  3.   until condition2;
  4.  
where each semicolon separates an empty statement (there is an empty statement after the "then".)  The example could have also been written as: 
Code: Pascal  [Select][+][-]
  1.   repeat
  2.     if condition1 then begin end
  3.   until condition2;
  4.  
and adding semicolons after the "end" would have the same effect (which is none) as in the previous example.


I find terminators instead of separators easier to understand.



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: if then else syntax
« Reply #5 on: June 03, 2023, 03:02:34 pm »
Yes I know.
But in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake).

440bx

  • Hero Member
  • *****
  • Posts: 4742
Re: if then else syntax
« Reply #6 on: June 03, 2023, 03:06:36 pm »
But in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake).
I do too.  I normally use a lone semicolon to indicate an empty statement.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: if then else syntax
« Reply #7 on: June 03, 2023, 04:37:44 pm »
Yes I know.
But in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake).

C/C++ programmers! :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018