1、猜:,王晓媛,VB 编 程,By,IF语句英译汉:,If the weather is good tomorrow, then I will go to the park.If the weather is good tomorrow, then I will go to the park, else I have to stay at home.,If then (如果那么),If then else (如果那么否则),VB中的选择语句 IF语句,用来解决有关选择结构的问题。,选择结构与选择语句,if 条件 then语句Aend if,if 条件 then语句Aelse语句Bend if,I
2、f和end if 要成对出现,例1:输入一个数到变量a,输出它的绝对值(不用绝对值函数),if _ then_end if,a0,a=-a,Print 意为打印,是输出语句。,a0,a = inputbox(“),print a,例2:从键盘分别输入两个不相等的数到变量a、b,输出其中较大的数。,if _ then_else_end if,ab,print a,print b,ab,练习1:输入两个不相等的数a,b,比较大小,将大数放在a中,小数放在b中,然后输出a,b,if _ then_end if,ab,t=a: a=b: b=t,VB程序代码中一行可以写多个语句,语句间用冒号隔开。,嵌
3、套分支结构,在分支结构的一个分支上再加入一个分支结构。类型有多种,比如:单分支嵌套单分支、双分支嵌套双分支等,嵌套分支结构,单分支嵌套单分支,双分支嵌套双分支,例3:已知分段函数y= ,输出y的值。,1 , x0,0 , x=0,-1 , x0,if _ then_ else if _ then_else_end if end if print y,x0,y=1,y=-1,x=0,y=0,分支结构1,分支结构2,分支嵌套双分支嵌套双分支,if 条件1 then语句1 else if 条件2 then语句2else语句3end if end if,分 支 结 构 1,分 支 结 构 2,注意:每
4、一层的end if结束本层判断。,练习2:,购买地铁车票,若乘14站,3元/位;若乘59站,4元/位;若乘10站以上,5元/位;输入人数person、站数n,输出应付款pay。,流程图,程序代码,if n=4 thenpay=3*person else if n=9 thenpay=4*personelsepay=5*personend if end if print pay,选择结构,单分支结构,双分支结构,嵌套分支结构,if 条件 then语句Aend if,if 条件 then语句Aelse语句Bend if,if 条件1 then语句1 else if 条件2 then语句2else语句3end if end if,小结,双分支嵌套双分支,温馨提示: 下节课在机房(F405)上课,记得带上练习纸和鞋套。,