相对于And,or最大的特性是会自动实现“最短路径"。
举例:
Private Sub test()
Dim a As Integer= 1
Dim b As Integer=2
'①
If a =1 and b = 2 then
end if
'②
If a =3 and b = 2 then
end if
'③
If a =3 andalso b = 2 then
end if
'使用and时,①在a,b同时为=true的情况下,判断了两个分支
'使用and时,②在a=fasle,b=true的情况下,还是判断了两个分支(如果在已知a=false的情况下,就跳出判断,更有效率。 但and任然会判断后一个分支)
'使用andalso时③在a=fasle,b=true的情况下,在得到第一个条件不成立时,不会再判断后面的分支,直接跳出判断
'①
If a =1 or b = 2 then
end if
'②
If a =3 or b = 2 then
end if
'③
If a =3 OrElse b = 2 then
end if
'同上
End Sub