ElseIf与ElseIf [英] ElseIf vs Else If
问题描述
多年来,我一直在使用Else If
在VBScript中进行编码...
For years now I've been using Else If
to code in VBScript...
If a = b Then
...
Else If a = c Then
...
End If
似乎可以按要求工作.除了使用ElseIf
的MSDN,我还看到了很多使用Else 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
.
ElseIf
与Else If
之间是否有区别?
<罢工> 这是我之前编写的,可以通过Classic ASP正常工作.
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.
忽略那个垃圾-我搞砸了搜索并在IDE中进行了替换.
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编译错误:预期为结束"
Microsoft VBScript compilation error: Expected 'End'
如我所料(如 @ ekkehard-horner
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
此方法的问题是,您最终在嵌套的If
语句上遇到未处理的条件.例如,如果a = d
会发生什么?
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?
您需要确保嵌套的If
捕获了ElseIf
语句不需要的额外条件.
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
方法应该是;
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.
这篇关于ElseIf与ElseIf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!