Bug 161883 - ElseIf is not equivalent to Else If
Summary: ElseIf is not equivalent to Else If
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Documentation (show other bugs)
Version:
(earliest affected)
7.3.7.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:25.2.0
Keywords:
Depends on:
Blocks: HelpGaps-NewFeatures Macro-Documentation
  Show dependency treegraph
 
Reported: 2024-07-03 14:48 UTC by ge.huber
Modified: 2024-08-14 16:23 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ge.huber 2024-07-03 14:48:48 UTC
The documentation states

"Instead of Else If you can write ElseIf, ..."

https://help.libreoffice.org/latest/ka/text/sbasic/shared/03090101.html

This seems to be untrue, see the following code where the Function CC1 using ElseIf works perfectly as expected, while CC is in a mess and should not even compile, because it as two End If

Copy the code into a module and execute Main 

Sub Main
MsgBox CC(0)
MsgBox CC1(0)
End Sub

Function CC(lNumber As Long) As String
Dim s1 As String
If lNumber <= 0 Then
    s1 = "lNumber <= 0"
Else If lNumber > 1 Then
    s1 = "lNumber > 1"
Else
    s1 = "Else"
End If
MsgBox s1
CC = s1
End If
End Function

Function CC1(lNumber As Long) As String
Dim s1 As String
If lNumber <= 0 Then
    s1 = "lNumber <= 0"
ElseIf lNumber > 1 Then
    s1 = "lNumber > 1"
Else
    s1 = "Else"
End If
MsgBox s1
CC1 = s1
End Function
Comment 1 Rafael Lima 2024-07-03 20:28:44 UTC
I guess the CC sub is working as expected. Maybe you placed the MsgBox in the wrong place (when compared to CC1). Below is your code with proper indentation:

Function CC(lNumber As Long) As String
    Dim s1 As String
    If lNumber <= 0 Then
        s1 = "lNumber <= 0"
    Else
        If lNumber > 1 Then
            s1 = "lNumber > 1"
        Else
            s1 = "Else"
        End If
        MsgBox s1
        CC = s1
    End If
End Function

So if you call CC(0) it will never reach the MsgBox nor will it set any return value.
Comment 2 Rafael Lima 2024-07-03 20:31:01 UTC
I guess what you wanted to do in CC is:

Function CC(lNumber As Long) As String
    Dim s1 As String
    If lNumber <= 0 Then
        s1 = "lNumber <= 0"
    Else
        If lNumber > 1 Then
            s1 = "lNumber > 1"
        Else
            s1 = "Else"
        End If
    End If
    MsgBox s1
    CC = s1
End Function

Notice the change in the position of "End If".
Comment 3 ge.huber 2024-07-04 06:07:05 UTC
(In reply to Rafael Lima from comment #1)
> I guess the CC sub is working as expected.

It is not about CC or CC1, but about the documentation. According to the documentation there should be no difference between Else If and ElseIf. But there is. If Else If really worked the same as ElseIf then CC should not even compile, because syntactically there is one End If too much.
Comment 4 Rafael Lima 2024-07-04 11:38:13 UTC
(In reply to ge.huber from comment #3)
> It is not about CC or CC1, but about the documentation. According to the
> documentation there should be no difference between Else If and ElseIf. But
> there is. If Else If really worked the same as ElseIf then CC should not
> even compile, because syntactically there is one End If too much.

Well if it's just that, I agree that this could be better documented.

When you use "Else If" you must have and "End If" However, when "ElseIf" is used, then you don't need and "End If" to close it.

I'm setting this to NEW.

@Alain what are your thoughts on this one?
Comment 5 Alain Romedenne 2024-07-23 16:39:05 UTC
This is a compiler/runtime bug

I noted that IF statement is not subject to specific unit tests although it is used in all tests using a primitive syntax IF .. ELSE ..
https://opengrok.libreoffice.org/xref/core/basic/qa/basic_coverage/

However help page documentation can still be improved
Comment 6 ge.huber 2024-07-23 17:36:59 UTC
To make this clear. I have no idea, what the intention was that Else If should work like ElseIf. So I'm totally fine, if only the documentation changes to reflect the fact, that it actually doesn't. I'm also fine, if the behavior changes, so that the documentation get's it right after all.

From my experience with Bug 80731 I'm slightly in favor of leaving the compiler as is, so as not to break code which might be out there, and only changing the documentation.
Comment 7 Alain Romedenne 2024-07-24 11:36:13 UTC
My Bad, this must be considered solely as a documentation issue.
Comment 8 ge.huber 2024-07-25 11:46:19 UTC
(In reply to Alain Romedenne from comment #7)
> My Bad, this must be considered solely as a documentation issue.

2nd that.
Comment 9 Commit Notification 2024-07-26 13:38:40 UTC
Alain Romedenne committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/help/commit/80cfa06de7bb382c30ead55837378dcd568d2e30

tdf#161883 Fix to IF Basic statement syntax diagram