6

I am now trying to explore pascal. And I ran into some compiler errors. I wrote a if else if statement like this:

  if ((input = 'y') or (input = 'Y')) then
    begin
      writeln ('blah blah');
    end;
  else if ((input = 'n') or (input = 'N')) then
    begin
      writeln ('blah');
    end;
  else
    begin
      writeln ('Input invalid!');
    end;

And it gives me an error at the first else:

";" expected but "ELSE" found

I looked for a lot of tutorials about if statements and they just do it like me:

if(boolean_expression 1)then 
   S1 (* Executes when the boolean expression 1 is true *)

else if( boolean_expression 2) then 
   S2 (* Executes when the boolean expression 2 is true *)

else if( boolean_expression 3) then 
   S3 (* Executes when the boolean expression 3 is true *)

else 
   S4; ( * executes when the none of the above condition is true *)

I tried to delete the begin and end but the same error occured. Is this a compiler bug?

P.S. I am doing this in a case statement. But I don't think it matters.

Sweeper
  • 213,210
  • 22
  • 193
  • 313

1 Answers1

10

; is not allowed before else in the majority of cases.

  if ((input = 'y') or (input = 'Y')) then
    begin
      writeln ('blah blah');
    end
  else if ((input = 'n') or (input = 'N')) then
    begin
      writeln ('blah');
    end
  else
    begin
      writeln ('Input invalid!');
    end;

will compile. But... Prefer using begin ... end brackets to avoid misunderstanding of code in complicated if then else statements. something like this will be better:

  if ((input = 'y') or (input = 'Y')) then
  begin
    writeln('blah blah');
  end
  else
  begin
    if ((input = 'n') or (input = 'N')) then
    begin
      writeln('blah');
    end
    else
    begin
      writeln('Input invalid!');
    end;
  end;

The second sample is much easier to read and understand, isn't it?

The code does not work when you remove begin and end because there is a semicolon before else. This will compile without errors:

  if ((input = 'y') or (input = 'Y')) then
    writeln('blah blah')
  else
  begin

  end;

Appended on comment of @lurker

Please, see the following example without begin ... end brackets.

  if expr1 then
    DoSmth1
  else if expr2 then
    if expr3 then
      DoSmth2
    else
     DoSmth3;//Under what conditions is it called?

It is not clearly seen here, if DoSmth3 is called on not (expr2) or (expr2) and (not (expr3)). Though we can predict the compiler behaviour in this sample, the more complicated code without begin ... end becomes subect to mistakes and is difficult to read. See the following code:

  //behaviour 1
  if expr1 then
    DoSmth
  else if expr2 then
  begin
    if expr3 then
      DoSmth
  end
  else
    DoSmth;

  //behaviour 2
  if expr1 then
    DoSmth
  else if expr2 then
  begin
    if expr3 then
      DoSmth
    else
      DoSmth;
  end;

Now the code behavior is obvious.

asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • BTW I am not sure about FPC vrsion but it supports `case` statement with `string` typed switcher. – Abelisto Oct 04 '15 at 14:50
  • @Abelisto Answering the question I was assuming that we were discussing only the syntax of `if then else` statement and the given sample was nothing but MCVE. Otherwise, we have to agree that the whole code including boolean expressions is really ugly from the point of Pascal programming standards. – asd-tm Oct 04 '15 at 16:17
  • It is suddenly that you mention my comment as criticism :) I just sign some another way to write such code. – Abelisto Oct 05 '15 at 07:25
  • 1
    @Abelisto Of course, not! Vice versa, I appreciate your comment! I was just explaining the reason why I have chosen this way to answer the question. I ever keep in mind one extraordinary solution for the following task. Imagine there is an integer variable taking values 9 and 10. How to assign 10 to it if it equals 9 and 9 if it is 10? We can use `if then else` or `case` statements. But the answer was `i:=19-i;` It is ever exciting to look for alike solutions.:-) – asd-tm Oct 05 '15 at 09:19
  • Why do you suggest that this code is complex enough to warrant an embedment of `if-else` constructs? I think this case is ideal for `if-else if-else` form which is what the OP originally had. It is an idiomatic form and there's nothing unclear about it. – lurker Oct 05 '15 at 12:51
  • @lurker. The current code is really simple and can be solved by tenths of ways. It is a question of taste actually. However, I suggest to consider other developed techniques that are used for such cases. See [the discussion](http://stackoverflow.com/q/32948001/5043424) that is currently rather active. And I pay the authors attention to the potential problems in more complicated cases. – asd-tm Oct 05 '15 at 12:58
  • @asd-tm for course there are many solutions. But to suggest that avoiding `if-else if-else` is clearer I think is misleading. I'm just saying if you are going to make a point about clarity, then a proper recommendation should be made. – lurker Oct 05 '15 at 12:59
  • @lurker Where did I tell to avoid `if then else`? I was saying that we were to think wider all the time and consider other ways that might be easier and faster. – asd-tm Oct 05 '15 at 13:02
  • @asd-tm when you said, *But... Prefer using `begin ... end` brackets to avoid misunderstanding of code in complicated `if then else` statements. something like this will be better:* and the following code eliminated the `if-else` construct in favor of a more verbose form. – lurker Oct 05 '15 at 13:03
  • @lurker I fairly don't understand how does the given quote proves your words, that I am insisting on avoiding `if then else`. The quote says that I advise to use `begin` ... `end` quotes to make the code easier to read but the number of `if`'s, `then`'s and `else`'s is the same :-) The last quote of my answer shows the answer to the question how to write a statement without `begin` ... `end` brackets. I simply skipped putting the same lines in the answer once again))) the other `if then else` statements were to be inside the `begin` ... `end` brackets. – asd-tm Oct 05 '15 at 13:10
  • @asd-tm the OP already was using `begin-end` and the only structural difference in your new code was the elimination of the `else-if` construct. So the strong implication is that replacing `else-if` with the more verbose form is clearer, which it isn't. Perhaps not what you intended to mean, but that's the way it reads. – lurker Oct 05 '15 at 13:13
  • @lurker Yes, he was. Though the syntax was incorrect and he could not compile the code and requested for help coming here. He met difficulties in understanding syntax with and without `begin` ... `end` brackets. Fortunateley the answer has helped him. The other is a matter of taste. The explanation of necessaity of `begin` ... `end` was an advice for the future as he will surely meet the difficulties in reading more complicated snippets containing such statements. – asd-tm Oct 05 '15 at 13:18
  • @lurker Please, kindly read the appended part of my answer. I tried to explain my position in it. – asd-tm Oct 05 '15 at 14:28