VB 78-选择与循环结构.pptVIP

  • 10
  • 0
  • 约4.31千字
  • 约 26页
  • 2018-01-14 发布于河南
  • 举报
VB 78-选择与循环结构

2.0 讲解实例 计算两个数的和、差、积、商 。。。 判断一个数是奇数还是偶数 判断一个年份是闰年还是平年 输入一个月份,输出季节的名称 计算器的设计 求1~100的和 计算斐波那契数列 2.4 程序的三种控制结构 顺序结构 条件分支 If If …then … else Select … Case 循环结构 For … Next Do [While|Until]… Loop [While|Until] While For Each … Next 顺序结构 顺序结构 顺序结构是一个常用的程序结构,由一系列的语句组成,按照顺序的先后从上至下执行。 Dim a As Integer Dim b As Integer Dim sum As Integer a = 10 b = 20 sum = a + b MsgBox(a+b= sum) 如以上语句按照程序编写的顺序从上到下执行,最后输出结果“a+b=30”。 IF分支结构 第一种是单分支结构 IF条件表达式 Then [语句1] End If If username = wmxing And password = 123456 Then MsgBox(验证通过!) End If 第二种是双分支结构 If 条件表达式 Then [语句1] [Else 执行语句2 ] End If 它表示:如果 条件表达式 值为True,那么就执行 语句1,否则执行语句2。 Dim scroe As Integer = 60 score = txtScore.Text If scroe =60 Then MsgBox(“及格) Else MsgBox(“不及格) End If If第三种结构的语法为 If 条件表达式 Then [语句1] [ElseIf 条件表达式 Then 执行语句2 ] [ElseIf 条件表达式 Then 执行语句3 ] [Else 执行语句n ] End If Dim score As Integer score = txtScore.Text If score 85 Then MsgBox(优秀) ElseIf score = 60 Then MsgBox(及格) Else MsgBox(不及格) End If 例:输入一学生成绩,评定其等级。方法是:90~100分为“优秀”,80~89分为“良好”,70~79分为“中等”,60~69分为“及格”,60分以为“不合格” 使用IF语句实现的程序段如下: If x=90 then MsgBox 优秀 ElseIf x=80 Then MsgBox 良好 ElseIf x=70 Then MsgBox 中等 ElseIf x=60 Then MsgBox 及格 Else MsgBox 不及格 End If Select/Case分支结构 Select Case testexpression Case condition_1 statements_1() [ … Case Else statements_n] End Select 示例 Dim Color As String = Red Select Case Color Case red MsgBox(You selected red) Case blue MsgBox(You selected blue) Case green MsgBox(You selected green) Case Else MsgBox(Unknown) End Select 使用select case…..语句来实现的程序段如下: Select Case x Case 90 to 100 MsgBox( “优秀”) Case 80 to 89 MsgBox( “良好”) Case 70 to 79 MsgBox (“中等”) Case 60 to 69 MsgBox (“及格”) Case Else MsgBox

文档评论(0)

1亿VIP精品文档

相关文档