"Komag" wrote:
I've wrangled with single line vs multi line IF handling, and it is definitely a unique beast. Different combinations of nested IF's, some single line and some multi-line, have to be handled with kid gloves I've found. Things that "should" work sometimes didn't. I don't know if I would call it a bug, just picky maybe.
It seems very tricky at first indeed. But "there is method to the madness" and when you get the knack of it, there should be no trouble.
Surely it could have been explained better - or at all - in the documentation. Currently there is no explanation whatsoever on the differences, instead one is left to guess it from the two examples. I suspect whoever wrote thought
there could be only one way to guess it,
well obviously duh... aa-and that's not the case! But it is easy to have blind spots when one is so close to the subject matter, can't blame them.
At first i wondered too - how does interpreter decide which one to stick with - could it be by look-ahead to see if `then` or `:` or `else` are used on the same line? That is not it though - `then` is unabashedly optional, can be skipped and rather it checks to see if there is another statement on the same line AFTER the IF. You can put any simple statement (that is, a non-block statement - FOR/WHILE are excluded, at least in current incarnation) on the same line following IF and that makes it a 1-liner; otherwise a block-IF will be assumed.
It was not a wise choice since causes confusion. If i were designing the language (and had to stick with BASIC), i would have distinguished the two IF forms more - likely by mandating that having "THEN" afterwards means 1-line IF and multi-line one is without `then` - just to be extra clear on who is who. But it is too late to fix it now.
Let me try to formalize the rules:
- At least one statement following IF on the same line determines it to be a 1-line-IF, otherwise it is a block-IF.
- THEN is optional after (any kind of) IF and does not affect the meaning
- Only simple-statements can partake in one-line-IFs (no block IF/FOR/WHILE)
- Multiple statements can follow IF on the same line, followed by an optional ELSE clause
- If ELSE keyword is present in 1-line-IF, it must be followed by one or more statements
PS. I just realized i am 1 letter short of spelling "ATOMIC" in
acrostic. Any ideas for a "C"-rule? :mrgreen: