Recent

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

GypsyPrince

  • Guest
[SOLVED] Conditional test statement/semicolon Hell!
« on: March 26, 2020, 09:33:14 am »
Just a general statement here...

Whomever decided the method for using/not using semicolons with If-Then and Case conditional test statements must have been eating mushrooms laced with LSD at the time.

A simple If block - no big deal. But for nested If blocks... kinda makes me want to seriously rethink learning this language. Could there possibly be a more flawed and headache inducing language feature?

Holy tomatoes, Batman!


Edit:
Programming languages are tools meant to be bent and shaped in whatever direction serves the whims of the person using them.  They are not holy relics to be worshipped in a glass case.  In other words... do your own thing that works for you and ignore the purists.  Do what ever works best for your particular situation and not necessarily whatever someone else thinks you should do.
« Last Edit: March 27, 2020, 03:29:49 am by GypsyPrince »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Conditional test statement/semicolon Hell!
« Reply #1 on: March 26, 2020, 09:39:07 am »
The important point to know is that Pascal uses the semicolon as a statement separator, not a terminator like C does. Thus one can drop the semicolon before an end and multiple semicolons are allowed as well. The semicolon also separates branches of a case-statement. And the syntax for an if-statement is if expr then block|stmt else block|stmt and neither a block, nor a stmt is terminated by a semicolon.

GypsyPrince

  • Guest
Re: Conditional test statement/semicolon Hell!
« Reply #2 on: March 26, 2020, 09:45:05 am »
@PascalDragon

It's frying my brain trying to keep them organized.

I'm going to need a HUGE bottle of aspirin...

LOL :o

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Conditional test statement/semicolon Hell!
« Reply #3 on: March 26, 2020, 09:49:05 am »
Where exactly do you have your problems? Because honestly I don't think about semicolons in 99% of the cases.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Conditional test statement/semicolon Hell!
« Reply #4 on: March 26, 2020, 09:49:37 am »
Just a general statement here...

Whomever decided the method for using/not using semicolons with If-Then and Case conditional test statements must have been eating mushrooms laced with LSD at the time.

Yes. Fixed in Pascal successor Modula2 in 1980.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Conditional test statement/semicolon Hell!
« Reply #5 on: March 26, 2020, 09:52:09 am »
Just a general statement here...

Whomever decided the method for using/not using semicolons with If-Then and Case conditional test statements must have been eating mushrooms laced with LSD at the time.

Take it up with Wirth, and with the designers of ALGOL in the late 1950s.

Quote
A simple If block - no big deal. But for nested If blocks... kinda makes me want to seriously rethink learning this language. Could there possibly be a more flawed and headache inducing language feature?

It's known as the dangling else problem. It was fixed in ALGOL-68, but Wirth chose not to fix it in Pascal although he did in Modula-2.

You can make life easier for yourself by using OTHERWISE rather than ELSE in a case statement.

Me, I treat  ;  as a strict separator which is how it was described in the books when I was learning the language. I'd not say that I never have problems with this, but unfortunately it's a language definition flaw that we all have to live with.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

GypsyPrince

  • Guest
Re: Conditional test statement/semicolon Hell!
« Reply #6 on: March 26, 2020, 09:54:09 am »
I use deeply nested If blocks.  Some have single statements while some have compound statements. For sake of not racking my brain, I'm simply putting begin/end in each level.  It's working now without compiler error, but my brain hurts.

Efficiency comes from consistency and repetition. I wish the Free Pascal developers would require the semicolon at the end of all statements, or none. Like it is, I don't think FP could be used in a high-production office setting.
« Last Edit: March 26, 2020, 09:56:14 am by GypsyPrince »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Conditional test statement/semicolon Hell!
« Reply #7 on: March 26, 2020, 10:01:13 am »
I use deeply nested If blocks.  Some have single statements while some have compound statements. For sake of not racking my brain, I'm simply putting begin/end in each level.  It's working now without compiler error, but my brain hurts.

Using beginend pairs is definitely a valid solution to avoid the potential pitfalls there...

Efficiency comes from consistency and repetition. I wish the Free Pascal developers would require the semicolon at the end of all statements, or none. Like it is, I don't think FP could be used in a high-production office setting.

Then those that successfully use Delphi/FPC in high-production office settings will disagree. :-X And while I might not use FPC officially at work (there we use C++) I can quickly pump out working Object Pascal code with no problems...

GypsyPrince

  • Guest
Re: Conditional test statement/semicolon Hell!
« Reply #8 on: March 26, 2020, 10:08:23 am »
@PascalDragon

Quote
Then those that successfully use Delphi/FPC in high-production office settings will disagree. :-X And while I might not use FPC officially at work (there we use C++) I can quickly pump out working Object Pascal code with no problems...

I'm certain when I get several more hours of FP/Lazarus under my belt I'll have a different attitude. Right not all my years of experience in C and wanting to terminate everything is working against me.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Conditional test statement/semicolon Hell!
« Reply #9 on: March 26, 2020, 10:18:43 am »
I wish the Free Pascal developers would require the semicolon at the end of all statements, or none. Like it is, I don't think FP could be used in a high-production office setting.

In that case it wouldn't be Pascal any more. This is a language /definition/ issue, not an /implementation/ issue... there's a few things that have been improved over the years (like tolerating ; in a couple of places where it was specifically prohibited by Wirth) but there's limits as to what can be done without breaking the language.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

GypsyPrince

  • Guest
Re: Conditional test statement/semicolon Hell!
« Reply #10 on: March 26, 2020, 10:46:43 am »
@MarkMLI

Quote
...but there's limits as to what can be done without breaking the language.

Actually, no, there's not. Just as spoken languages are allowed to evolve, so are programming languages. Programming languages/environments are tools meant to serve the programmer - nothing more.  They certainly are not holy relics.  Implementing the option to terminate each statement with a semicolon (I stress the word "option", as it would not be a requirement) would certainly not "break" the language.  Is there some regard for Pascal as a holy relic? would it really hurt someone's feelings if a few things were changed to make it more user-friendly and efficient?  I certainly hope not because Shakespeare would be rolling over in his grave if he knew the extent to which the English language has changed since he helped formalize it.

Again, a programming language is simply a tool to be used as needed. It's not something to be worshipped or guarded against change.

440bx

  • Hero Member
  • *****
  • Posts: 4029
Re: Conditional test statement/semicolon Hell!
« Reply #11 on: March 26, 2020, 10:49:10 am »
I wish the Free Pascal developers would require the semicolon at the end of all statements, or none. Like it is, I don't think FP could be used in a high-production office setting.
A lot of Pascal compilers "tolerate" semicolons where there aren't supposed to be any. 

Neither "end" nor "else" are statements (or begin a statement), therefore there should not be a semicolon before them.  In the case of "end" the compiler has been "trained" to tolerate them but, in the case of "else" allowing a semicolon would change the semantics, therefore that's a no-go.

You'll get used to semicolons soon enough. 

A rare but, much more annoying problem with Pascal's design is the use of "begin"/"end"  to mark the beginning and end of function/procedure blocks while also using them as compound statement "delimiters".   Combine that with the fact that functions and procedures can be nested and that opens the door to some really annoying problems.  For instance
Code: Pascal  [Select][+][-]
  1.   program FunWithBeginEnd;
  2.  
  3.     procedure ProcedureName;
  4.  
  5.       procedure AnotherProcedure;     { procedure has no blockbody       }
  6.  
  7.     begin       { Top to bottom associates with AnotherProcedure, bottom }
  8.     end;        { to top, associates with ProcedureName.                 }
  9.  
  10.   begin
  11.     begin
  12.     end;
  13.   end.
  14.  
When there aren't hundreds of lines of code in the way, it's easy to see the problem but, in a large program it's a different thing.  A much more common problem is the "extra" begin or the "missing" end.   The compiler gets to the very bottom of the source and "informs" you that it expected a semicolon.  If you've made more than a few changes you can still remember then you're in for some fun.



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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Conditional test statement/semicolon Hell!
« Reply #12 on: March 26, 2020, 11:14:25 am »
As part of the bigger picture, also that "classic" Pascal uses e.g.

Code: [Select]
if <condition> then
  <single statement>
else
  <single statement> ;

while newer features use e.g.

Code: [Select]
try
  <statement sequence>
finally
  <statement sequence>
end;

There is of course also the overloading of  try  which can herald both a  try-finally  or a  try-except  statement (I'd prefer  start-finally  as an alternative). And many other problems.

I agree that the language should be reworked, particularly since Wirth admitted Pascal's flaws by eliminating some of them in Modula-2. Ain't gonna happen.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Conditional test statement/semicolon Hell!
« Reply #13 on: March 26, 2020, 11:57:49 am »
@MarkMLI

Quote
...but there's limits as to what can be done without breaking the language.

Actually, no, there's not. Just as spoken languages are allowed to evolve, so are programming languages.

That's because humans are more forgivving about little changes than machines. Unfortunately this does not work.

Quote
Programming languages/environments are tools meant to serve the programmer - nothing more.  They certainly are not holy relics.  Implementing the option to terminate each statement with a semicolon (I stress the word "option", as it would not be a requirement) would certainly not "break" the language.

Before the ELSE, the semicolon or not has meaning. Search for the already mentioned phrase "dangling else"


Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Conditional test statement/semicolon Hell!
« Reply #14 on: March 26, 2020, 12:23:06 pm »
I do not against FP/Pascal to evolve for better, but I never have problem with conditional test statement.

One thing for sure is, I always avoid deeply nested if block. And I sometimes combine if block with case statement. That works for me.
« Last Edit: March 26, 2020, 12:39:52 pm by Handoko »

 

TinyPortal © 2005-2018