- 3
- 0
- 约3.99千字
- 约 7页
- 2018-02-07 发布于河南
- 举报
第五章 vb控制结构
第五章 vb控制结构
1.If 语句
(1)If 条件 then
语句块
End if
例:输入两个数x和y,编程使得x的值始终大于y的的值
Dim x as integer
Dim y as integer
x=val(text1.text)
y=val(text2.text)
If xy then
t=x: x=y:y=t
End if
Text3.text=x
Text4.text=y
(2)If 条件 then
语句块1
Else 语句块2
End if
例:输入x,计算y的值,其中y=x+1(x0),y=2x-1(x=0)
Dim x as integer
Dim y as integer
x=val(text1.text)
If x0 then
y=x+1
Else
y=2*x-1
End if
Text2.text=y
(3)If 条件1 then
语句块1
Elseif 条件2 then
语句块2
…
Else
语句块n
End if
例:某购物商场超市节日期间举办购物大折扣活动,优惠方法:每位顾客当天一次性购物在100元以上者,按九五折优惠;在二百元以上者,按九折优惠;在三百元以上者,按八五折优惠;在五百元以上者,按八折优惠。根据顾客购物款项计算出优惠价。(购物总价格,优惠价格)
Dim a as single
Dim b as single
a=val(text1.text)
if a100 then
b=a
elseif a200 then
b=a*0.95
elseif a300 then
b=a*0.90
elseif a500 then
b=a*0.85
else
b=a*0.80
end if
text2.text=b(select case语句)
(4)if 语句的嵌套
If 条件1 then
If 条件11 then
…
End if
End if
例:比较三个数x,y,z的大小顺序,使得xyz.
Dim x as integer
Dim y as integer
Dim z as integer
If xy then
t=y:y=x:x=t
If xz then
t=z:z=x:x=t
If yz then
t=z:z=y:y=t
end if
end if
end if
2.select case语句:select case 测试表达式或变量
case 表达式列表1
语句块1
case 表达式列表2
语句块2
…
case else
语句块
End select
注:case表达式形式:1.case 100*a表示数值或字符串表达式 2.case 1 to 10 表示一个值的变化范围3.case 1,2,3,….枚举表达式的某个值4.case is100表示给定一个数的范围
例:Dim a as single
Dim b as single
a=val(text1.text)
select case a
case is100
b=a
case is200
b=a*0.95
case is300
b=a*0.90
case is500
b=a*0.85
case else
b=a*0.80
end select
text2.text=b
例:select case x
Case 2
Print 28
Case 1,3,5,7,8,10,12
Print 31
Case else
Print 30
End select
3.IIf函数
resul=iif(条件,true部分,false部分)
a=iif(r5,1,2) 等于
4.for循环
For 循环变量= 初值 to 终值(step步长)
(循环体)
(exit for)Next (循环变量)
例:求1+3+5+…+99的累加和
Dim s as integer
Dim i as integer
S=0
For i=1 to 99 step 2
s=s+i
print “s=”,s
next i
5.while 循环
While 条件
(语句块)
Wend
6.do 循环
(1)先判断型do…loop循环
①do while…loop“是循环” ②do until…loop “否循环”
例:用循环语句求1+2+3+…+100的累加和
①do while…loop
Dim s as integer
Dim i as integer
S=0
i=1
do while i=100
s=s+i
i=i
原创力文档

文档评论(0)