B4J Question Else If (expression) Then (expression) - in 1 line

a n g l o

Active Member
Licensed User
Longtime User
the following code :
B4X:
Dim a As Int
a=2
If a =1 Then
    Log(1)
Else If a=2 Then Log(2)       
End If
does not raise any red issues / warning issues from the IDE (b4J).
but it does NOT log "2" (as expected ??).
only if the 'log(2)' moved to next line - it logs it ok.

so, if writing this way is a syntax mistake/error - the IDE should warn you about that.
and if not, it should work (as expected ??).

BTW - just tried it on vb6 ide - it's accepted and works there. (not that it binds b4x in any way).

if i'm missing something, please let me know. thank you.
 

agraham

Expert
Licensed User
Longtime User
No it's not a parser failure. Single line If ... Then ... has always been allowed in B4X. Check the Intellisense for If
Single line:
If condition Then true-statement [Else false-statement]
Multiline:
If condition Then
statement
Else If condition Then
statement
...
Else
statement
End If
Though if does seem to not work as expected after an Else. So while not necessarily a parser failure it does look like a bug of some sort!
 
Upvote 0

PaulMeuris

Active Member
Licensed User
You could also write it like this:
if on one line:
    Dim a As Int
    a=2
    If a =1 Then
        Log(1)
    Else
        If a=2 Then Log(2)
    End If
This way the single line if is on one line and nothing preceeds it.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
But the parser should understand that it doesn’t apply to this case !
Why shouldn't it apply in this case? Other than Erel decided not to support it in which case it should produce a warning. Only Erel can tell us what the original intent was.🤔
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1658038766550.png
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
It is considered invalid syntax. I will check why there is no parser error...
I told you it’s a parser failure. There’s are other cases like this one. I don’t remember but I will post if I encounter them.
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
Erel thank you,
as for the image you uploaded :
no such red error marks here (b4j 9.8).
i belive i'm not the only one here that can say that.

i'd like to know what to do to have that error marks as well.
it may seems a little issue but belive me, it's not :

it is a project where there are more then 1700 'Else If'.
on friday i've found 1 case where the following 'Then' expresion is in the same line.
AS THERE IS NO ERROR from the IDE about that, AND the 'Then' part is ignored (treated as comment....) ==>
==> the algorithem produce a wrong calulation/result.

now, if i found 1, there maybe more.
i'd like the IDE to point me to them, otherwise, i'll have to write code to find syntax errors....

Thank you
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
it is a project where there are more then 1700 'Else If'.
The very large number of IF / ELSE statements reminds me of the time when we had to "normalize" such a function in school, so that the duplications were removed and a simplification occurred. You often see that a specific condition often excludes several possible. This greatly reduces the number of tests.
 
Upvote 0
Top