If .. Then .. : (colons) ...

Forum for discussion about the documentation project.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

If .. Then .. : (colons) ...

Post by Tourist Trap »

Hello

the doc at If...Then shows this syntax:
If expression Then [statement(s)] [Else [statement(s)]] [End If]
-----> no colons allowed ?
-----> statement is optional?

or

If expression Then : [statement(s)] [Else [statement(s)]] : End If -----> end if mandatory with colons?

or

If expression Then
[statement(s)]
[ ElseIf expression Then ]
[statement(s)]
[ Else ]
[statement(s)]
End If
So the following case (one of the most ambiguous) is not documented. At least I havent' been able to find the answer by reading:

Code: Select all

dim as integer i = 0

if i=1 then ? "in ": ? "the " : ? " if" : ? 

? "out of if"

sleep
I know that this is difficult to make a compact syntax in the syntax description. Here I don't know how it could include more, unless maybe distinguishing the "single-line IF" and the "multi-line IF" ? I think that if the Statement is indicated ad optional in the first case, it's maybe because it's still possible to see there the start of a multi-line. It's annoying because then, the description of a single-line IF is not complete (case of colons).

Thanks.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: If .. Then .. : (colons) ...

Post by fxm »

Tourist Trap wrote:So the following case (one of the most ambiguous) is not documented. At least I havent' been able to find the answer by reading:

Code: Select all

dim as integer i = 0

if i=1 then ? "in ": ? "the " : ? " if" : ? 

? "out of if"

sleep
This case is well documented by:
'If expression Then [statement(s)] [Else [statement(s)]] [End If]'
where '[Else [statement(s)]]' and '[End If]' are not used
(for all syntaxes, the expressions inside brackets are optional)
so:
'If expression Then statements'

Code: Select all

#define expression i=0
#define statements ? "in ": ? "the " : ? " if" : ?

dim as integer i = 1

If expression Then statements

? "out of if"

sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: If .. Then .. : (colons) ...

Post by Tourist Trap »

fxm wrote: (for all syntaxes, the expressions inside brackets are optional)
Look this expression in bold is inside brackets:
If expression Then [statement(s)] [Else [statement(s)]] [End If]
So it seems possible to write no more no less than:

Code: Select all

if expression then
The second point is not for abusing your patience, but "statement(s)", sincerely doesn't make this obvious that this means also something containing series of statements separated with commas, AND without END IF. It is in contradiction with the second line.
And the second line itself seems broken (just focus at the comma):

Code: Select all

#define expression i=0
#define statements ? "in ": ? "the " : ? " if" : ?

dim as integer i = 1

If expression Then : statements

? "out of if"

sleep

Code: Select all

error 32: Expected 'END IF'
In fact this is even more difficult to guess that "statement(s)" hide commas, when in the second line the comma is exhibited (and broken?).

And in all case, the problem is with the "commas", and the fact that they allow omitting END IF.

This is something that I wouldn't do myself, but I've found examples of codes with those syntax and I simply couldn't know where the IF ended. Even worst, I could rewrite two version of the code that compiles depending on the choice made, but obviously breaking the code if misinterpreted:

Code: Select all

'example from the forum
While I <= J
    While array(I).z > X .z:I+=1:Wend
    While array(J).z < X .z:J-=1:Wend
    If I<=J Then Swap array(I),array(J): I+=1:J-=1
Wend
As you see that can be very involved to read an algorithm if the rules are not really clear with single line IF. You can notice that the While bloc would require a END WHILE. So this is not possible to infere the right rule for IF by simply comparing. The doc is in this case mandatory.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: If .. Then .. : (colons) ...

Post by fxm »

I will stop to answer to this topic because I think that the current page is a good compromise between be clear/conscious and exhaustive.
If another wants to take over ...!
(Yet I am patient generally, but here I reach my limit)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: If .. Then .. : (colons) ...

Post by Tourist Trap »

fxm wrote: (Yet I am patient generally, but here I reach my limit)
It's not specially to your address fxm. It's a user remark about the documentation. It doesn't mean that this is either right, or to be corrected. And you can also have hollydays!

In general anyway, I personnally think that sometimes the syntax is too compact. Here my attempt for removing accidental obfuscation:
If expression Then statement [ : statement(s)] [Else [statement(s)]] [End If]
or
If expression Then : [statement(s)] [Else [statement(s)]] : End If
or
If expression Then
[statement(s)]
[ ElseIf expression Then ]
[statement(s)]
[ Else ]
[statement(s)]
End If
The syntax is expressed in a sort of compressed scheme. If it takes minutes to unzip it, the best is to unzip it a little. It's far better than leaving the common user in pain.

Maybe [statement(s)] has to really be replaced everywhere here by:
statement [ : statement(s)]
Thanks for the help, and sharing ;-)
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: If .. Then .. : (colons) ...

Post by fxm »

Tourist Trap wrote:In general anyway, I personnally think that sometimes the syntax is too compact. Here my attempt for removing accidental obfuscation:
If expression Then statement [ : statement(s)] [Else [statement(s)]] [End If]
or
If expression Then : [statement(s)] [Else [statement(s)]] : End If
or
If expression Then
[statement(s)]
[ ElseIf expression Then ]
[statement(s)]
[ Else ]
[statement(s)]
End If
The syntax is expressed in a sort of compressed scheme. If it takes minutes to unzip it, the best is to unzip it a little. It's far better than leaving the common user in pain.

Maybe [statement(s)] has to really be replaced everywhere here by:
statement [ : statement(s)]
Not that easy:
if expression then else statement(s)
is valid.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: If .. Then .. : (colons) ...

Post by Tourist Trap »

fxm wrote: Not that easy:
if expression then else statement(s)
is valid.
I had never noticed that before:

Code: Select all

var a = 2
If 1=1 Then   Else a=3 
If 1=1 Then   Else          ''OK!!
If 1=1 Then                   ''wrong
So it seems that the first sentence is expanding in those two cases:
If expression Then statement [ : statement(s)]
or
If expression Then [statement(s)] Else [statement(s)] [End If]
The case of "If then else" totally empty is quite peculiar! It maybe has some use inside macros... But less ambiguity maybe with:

Code: Select all

If 1=1 Then:   Else :   End If   ''OK

If 1=1 Then   Else    End If        ''OK
It's just for the fun of course, but now it seems that ELSEIF is not excluded from single line version2:

Code: Select all

If 1=1 Then :  ElseIf 2=2 Then :   Else:   End If         ''OK
This would maybe be rendered by:
If expression Then : [statement(s)] [ ElseIf expression Then :] [Else [statement(s)] :] End If
...
I think I've found the trick then:
If expression Then statement [ : statement(s)]
or
If expression Then [statement(s)] Else [statement(s)] [End If]
or
If expression Then [ : [ [statement(s)] [ {[ ElseIf expression Then : ]} Else [statement(s)] :]] ] End If
or
multiline
Who has said that the most frequent keywords are the simplest ;-)
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: If .. Then .. : (colons) ...

Post by petan »

Nice, I didn' know this, thx.

But, on the other hand, I see high risk/potential troubles, if in such compressed code would be ANY kind of mistake - typo/math-stat-logical error etc.
Surely longer time for debugging.
Surely longer time for upgrading such code after years. ("What is the job of this long line ?")
Problematic placing programmer's comments for such long compressed codeline.

So, better 2x measured/thinked this 'IF' line , then 1x written...;)
Anyway, good catch !
Post Reply