vb中什么情况用If 和case语句有什么区别

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:20:35
觉得两者没太大区别,请问高手它们两者可以通用吗?在什么情况下侧重点是哪个?
我看得很迷糊,教材上只讲两者各自的应用,那请问一道问题两个函数有可能同时用各自的方法表示出来吗?这

我还是打吧.....
case属于多分支结构,if可用于单分支和多分支
case在单变量多分支的情况下和IF可以互换且case要好些,更直观,更有可读性。在多个变量的时候只能用if
举例说明吧
if x>0 and y>0 then
msgbox("在第一象限")
ekseif x<0 and y>0 then
msgbox("在第二象限")
....

这儿就不能用case,因为
select case 后面只能跟一个变量

量少用IF,量多用Case,看一下代码就很清楚了。

例1:量多时
sub command1_click(index as integer)
select case index
case 0
程序
case 1
程序
case 2
程序
else
end select
end sub

sub command1_click(index as integer)
if index=0 then
程序
elseif index=1 then
程序
elseif index=2 then
程序
else
程序
end if

end sub

量少时(这里就1个)

select case index
case 0
程序
else
程序
end select

if inde=0 then
程序
else
程序
end if

if是if,case是case

If 多用于少量选项,Select Case 多用于大量选项

c