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?
(Deeply) nested IF blocks (no matter of where semicolons are allowed or forbidden) are always messy. In any language.
Some language have mandatory "blocks" around the conditional code. Like "then ... ENDIF", if ... fi (reverse if), or "{}".
Pascal offers begin end, just not mandatory.
Still even with blocks, in any language deeply nesting "if" gets messy.
Indent can help. But very few language have a mandatory indent.
In Pascal, if you place a semicolon incorrectly before a "else" you get an error (except if you are nested in a "case", and the case block has no begin/end).
The only danger is that you place a semicolon immediately after then or else (but see further down, for highlighting such error)
if a= b then ;
DoEqual; // unconditional, because the line above had an empty statement for the conditional code.
But then, other languages that do not have mandatory blocks, will in most cases behave exactly like that.
Writing readable if/then code is not so much about what tokens are mandatory. Writing in a language with mandatory {}, still allows the programmer to write messy code.
if (foo)
{ if (bar) { a=1;
c=a; }
else
{ b=1;
}}
Design your code to avoid deeply nested if.
And where that fails, use an editor (such as the Lazarus IDE} that has plenty of highlighting to show you were each if/else ends.
At least if you write each statement on a new line, and keep some indent.
- You get vertical colored lines. (see image) / Also nested "if" has a different color. And ";" are marked, if they belong to (the end) of an if/else
- You can add color for common errors (my IDE is set to mark certain errors in red, see image)
To get the error, go to "user defined markup)
https://wiki.lazarus.freepascal.org/IDE_Window:_Editor_User_Defined_WordsAnd add terms like "then;" and "then ;" / "then ;" with a color of your choice.