比如以下两种写法他们的执行效率是不是都一样啊?第一种:
If condition1 Then
statementsEnd If
If condition2 Then
statementsEnd If
If condition3 Then
statementsEnd If
第二种:
If condition1 Then
[statementblock-1]
[ElseIf condition2 Then
[statementblock-2]] ...
[Else
[statementblock-n]]End If
If condition1 Then
statementsEnd If
If condition2 Then
statementsEnd If
If condition3 Then
statementsEnd If
第二种:
If condition1 Then
[statementblock-1]
[ElseIf condition2 Then
[statementblock-2]] ...
[Else
[statementblock-n]]End If
'do
endifif true then
'do
end if if false then 'do'_____________________________________if true then
'do '只有这个部分会被执行。
elseif true then
'do
elseif false then
'do
else
'''''''''''''''''''''
endif
case condition1
Code1
case condition2
Code2
case condition3
Code3
.
.
.
case conditionN
CodeN
End Select
第一种写法在某些特定时候会稍微容易理解一些,在不要求效率的时候用一下也无所谓
但必须注意condition1 、2、3可能同时成立多个,这样两种写法执行结果就不一样了。
xx2=yy2 and _
xx3=yy3 and _
xx4=yy4 then
'do
end if
这样?
1、第一种的每一个判断执行完后,都要进行后面的判断,后面的判断是多余的;而第二种则不会,每执行完一个判断后,则直接结束判断。
2、对于有多个变量的判断用第二种是最有效的。
3、对于一个变量有多个值的情况下,用Select Case是最有效的。
就代码上看,你上下两段的意义不同的哦。上面的可能多个if独立进入,可以进入多次,下面的只有一个if分支可以执行。