Recent

Author Topic: [SOLVED] Conditional test statement/semicolon Hell!  (Read 8403 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9870
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #60 on: March 28, 2020, 12:07:52 am »
Not going to repeat myself, answered this before (and so have you it seems). Lets agree to disagree.

Conclusion (both):

I do see (and read the docs that way) the entire "if then embedded else embedded" as one statement. There is no terminator nor separator possible inside it.

I do not do "while test(a) ; {while finished, terminate/separate it from whatever follows } do inc(a);"
And that "while" example is not just dreamed up. That is (similar to) how bash does it.


I understand (or assume) you read the fpc docs differently, and therefore have a different view and what is the "if then" and what is the "embedded" statement.
Assuming such a different view, your initial statement is correct.



Btw:
Code: Pascal  [Select][+][-]
  1. begin
  2.   If a then
  3.     foo
  4.   else
  5. end
Here I agree, a terminator (rather than a separator) would require a ; for the empty else statement.
Same for an empty "then" in case there was no "else".

We just differ in what the terminator terminates...



Last minute edit:

I do not see any reference in the docs, that "else" is in any form a statement separator.

So in case the "then_statement" And the "else_statement" were separate statement, they would need a separator. (because that is what you have between 2 statements).

« Last Edit: March 28, 2020, 12:13:31 am by Martin_fr »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #61 on: March 28, 2020, 12:37:49 am »
Hi!

There was once (in the 70s) the discussion from Pascal students:

"Where to put begin end"

N. Wirth pointed out: For one statement you don't need them.
And he pointed out that if is one instruction.
And the same with if then else.

So you can build if cascades without begin end.

Winni

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9870
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #62 on: March 28, 2020, 01:23:48 am »
And he pointed out that if is one instruction.
And the same with if then else.

Well, and it is my opinion, that within (the middle of) one (a single) statement there cannot be a statement terminator. (same as there cannot be a separator).

But there I am, repeating myself (again).


I am aware that begin...end is a single (combined) statement too.
But for this it is explicitly defined that the separators apply to the included statements (and if it where terminators, the same explicit definition would have to exist).
« Last Edit: March 28, 2020, 01:27:47 am by Martin_fr »

440bx

  • Hero Member
  • *****
  • Posts: 4031
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #63 on: March 28, 2020, 01:24:47 am »
Not going to repeat myself, answered this before (and so have you it seems). Lets agree to disagree.
No problem.  I'll agree to disagree but, I do want to point out that I was _explaining_ not arguing about separators and terminators.

I will provide a last example, as a last effort, not as an argument.  In the statement:

if a = b then b := a else a := b;

In a language, such as C, that uses statement terminators, the statement "b := a" after the "then" would require a terminating semicolon.  I believe you see that.  Now, the reason Pascal doesn't require it in that case is because the semicolon is not a terminator and "else" is not a statement, therefore, no semicolon.  And, because in Pascal, the semicolon is a separator, including one after "b := a" will result in the compiler emitting an error because "else" is not a statement, therefore it doesn't need to be separated from "b := a".

Conclusion: I agree to disagree and the above is my last effort to explain it.

I do not see any reference in the docs, that "else" is in any form a statement separator.
"else" is neither a statement nor a separator, it is simply a _part_ of an "if" statement.  Like "to" in a "for/to/do" statement.

So in case the "then_statement" And the "else_statement" were separate statement, they would need a separator. (because that is what you have between 2 statements).
yes but, that wouldn't make any sense.  An "else" by itself doesn't have anything to "else" from.

ETA

the inner ";" terminates the inner statement not the outer one which is the "if", that statement will be terminated by the ";" that ends the statement in the "else" part.  If there was no "else" part then the semicolon would terminate the "if" as well.


« Last Edit: March 28, 2020, 01:28:27 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9870
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #64 on: March 28, 2020, 01:54:40 am »
Now, the reason Pascal doesn't require it in that case is because the semicolon is not a terminator

Now that is where I disagree.

I believe if that you if you take all Pascal definitions exactly as the are. And only change that ; is a terminator => then (in Pascal) there would still not be a ; in front of the else.

Because if there was, it would terminate the IF statement (that is how "IF" IMHO is defined). So there cannot be a terminator.


You (seem to ) believe  that "if" is defined in such a way that it would not be terminated by a terminator in that Place. I do not see how to derive that from "if then else" is one single statement.

But this is where  we disagree.

The separator/terminator disagreement, IMHO stems from that different view.

Quote
the inner ";" terminates the inner statement not the outer one which is the "if", that statement will be terminated by the ";" that ends the statement in the "else" part.  If there was no "else" part then the semicolon would terminate the "if" as well.
You said that before. That may be true for C.

I do not see that this would be a given in Pascal. Of course currently it is not defined for Pascal, because Pascal has no terminators.

But then, if you switched Pascal to terminators, you would need to add this sort of inner/outer definition.

You would thereby change the definition of "IF".
Because, currently the embedded statement become part of the outer IF. So they are no longer statements on their own. (and not requiring a terminator) Otherwise the "IF" would not truely be one statement, but that is how it is defined in Pascal.

That can be compared to the following C: "if ( (a=b) ==0) {}".  "a=b" is/was a statement, but is now part of the expression. It no longer gets a terminator.
« Last Edit: March 28, 2020, 01:58:08 am by Martin_fr »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [SOLVED] Conditional test statement/semicolon Hell!
« Reply #65 on: March 28, 2020, 02:33:53 am »
Hi!

The discussion comes to a sudden death if you ask:

How to use then or else without if ???

There you go.

And don't tell me about the "case else". That is bloody Turbo Pascal syntax rubbish.
It is called otherwise.

Winni

 

TinyPortal © 2005-2018