For years now I've been using Else If
to code in VBScript...
If a = b Then
...
Else If a = c Then
...
End If
Which seems to work as required. I've also seen many sites on the web that use Else If
, excepting MSDN that uses ElseIf
.
Is there a difference between ElseIf
versus Else If
?
Here's one I coded earlier that's working just fine through Classic ASP:
If IsDate(wD) Then
wS = wD
Else If wD&"" <> FormatDisplayDate(wS) Then
wS = WeekStart(Date())
wD = FormatDisplayDate(wS)
End If
Here's a snippet from an older piece of code, written by someone else...
if opip = "IP" then
opip = "In Patient"
Else If opip = "OP" then
opip = "Out Patient"
End If
None of these are run through a compiler, however, it's all interpreted.
Ignore that junk - I messed up a search and replace in the IDE.
That example code doesn't compile and produces the compilation error
Microsoft VBScript compilation error: Expected 'End'
as I'd expect (as @ekkehard-horner point's out in the comments).
I've never known ElseIf
to work any other way then detailed in MSDN. The only thing I can think of is you are writing it as a nested If
statement.
If a = b Then
...
Else If a = c Then
...
End If
End If
which looks really ugly but is the same as writing
If a = b Then
...
Else
If a = c Then
...
End If
End If
Problem with this approach is you end up with an un-handled condition on the nested If
statement. What happens if a = d
for example?
You would need to make sure your nested If
caught the extra condition which isn't needed with an ElseIf
statement.
If a = b Then
...
Else
If a = c Then
...
Else
...
End If
End If
ElseIf
approach would be;
If a = b Then
...
ElseIf a = c Then
...
Else
...
End If
Interesting musing by @eric-lippert (one of the programmers behind the VBScript compiler) in the sea of comments...well worth a read. I certainly learned something.