版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Tutorial 4,1,Tutorial 4,The selection structure,Tutorial 4,2,一、Programming Structures程序结构,Computer scientists have defined three constructs for a structured program. Sequence 顺序结构 Selection - (Decision) 选择(分支)结构 Repetition 循环结构,Tutorial 4,3,1. Sequence顺序结构,program instructions are processed, one aft
2、er another, in the order in which they appear in the program 语句按照出现的先后顺序一条条执行,Tutorial 4,4,Tutorial 4,5,Private Sub Form_Click() frmGame.Refresh frmGame.FontName = 宋体 frmGame.FontSize = 10 frmGame.Print 游戏开始了 frmGame.FontName = 华文行楷 frmGame.FontSize = 15 frmGame.Print 欢迎进入俄罗斯方块游戏 End Sub,Tutorial 4,
3、6,2. Selection Structure选择结构,程序设计时常常需要对给定的条件进行分析、比较和判断,并根据判断的结果执行不同的语句块。 当程序执行到具有选择结构的语句时,先测试条件,然后根据测试结果从不同的分支中选择某一分支执行。这里作为测试的条件又称条件表达式,其值只有True/False两值。,Tutorial 4,7,Selection Structure Pseudocode,If condition is true Then perform these tasks End If Perform these tasks whether condition is true or
4、 false,Tutorial 4,8,If condition is true then perform these tasks Else perform these tasks End If Perform these tasks whether condition is true or false,条件为真吗,T,F,语句1,语句2,语句,Tutorial 4,9,Flowchart Symbols,start/stop oval process rectangle input/output parallelogram selection/repetition diamond symbo
5、ls are connected by flowlines,Tutorial 4,10,Selection Structure Flowcharts,Tutorial 4,11,condition条件,关系运算符 逻辑运算符 只有True和False两值,Tutorial 4,12,(1) 关系运算符,关系运算又称比较运算,用来对两个数或两个表达式进行比较。关系运算符和运算数构成关系表达式,其格式如下:expression1 关系运算符 expression2,Tutorial 4,13,Relational Operators关系运算符,= = ,Equal to Greater than
6、Greater than or equal to Less than Less than or equal to Not equal to,Tutorial 4,14,如果比较的数值类的数据,将根据数值的大小进行;如果比较字符串,将根据字符串的ASCII值进行;如果比较的是表达式,先计算出表达式的值,再进行比较。 关系运算符的优先级相同,结合顺序从左到右。关系运算的结果是逻辑型的数,即True/False。,Tutorial 4,15,34 “ab”(42+14),Tutorial 4,16,Expressions Containing Relational Operators,10 + 3
7、5 * 2 5 * 2 is evaluated first, giving 10 10 + 3 is evaluated second, giving 13 13 10 is evaluated last, giving false,7 3 * 4 / 2 3 * 4 is evaluated first, giving 12 12 / 2 is evaluated second, giving 6 7 6 is evaluated last, giving true,Tutorial 4,17,(2) 逻辑运算符,逻辑运算又称布尔运算,对两个以上的关系式或逻辑变量进行逻辑运算,运算的结果只
8、有True/False两值。逻辑运算符和关系运算符一起构成了程序的条件判断部分。,Tutorial 4,18,Visual Basic提供了6种逻辑运算符,按优先顺序分别是:Not(非) And(与) Or(或) Xor(异或) Eqv(等价) Lmp(蕴含)。 逻辑运算符的优先级低于关系运算符。,Tutorial 4,19,a. Not(非),Not是逻辑取反运算。如表达式Not(34)的值是True:先计算出括号内关系式的值是False,取反之后的值是True。,Tutorial 4,20,Truth Table for the Not Operator,Tutorial 4,21,b.
9、And(与),当所有关系式的值皆为True时,And运算的结果是True;当任何一个关系式的值为False,And运算的结果为False。,Tutorial 4,22,Truth Table for the And Operator,True:1 False:0 0*0=0 0*1=0 1*0=0 1*1=1,Tutorial 4,23,c. Or(或),当所有关系式的值皆为False时,Or运算的结果是False;当任何一个关系式的值为True,Or运算的结果为True。,Tutorial 4,24,Truth Table for the Or Operator,True:1 False:0
10、 0+0=0 0+1=1 1+0=1 1+1=1,Tutorial 4,25,Expressions Containing the And Logical Operator,3 2 And 6 5 3 2 is evaluated first, giving true 6 5 is evaluated second, giving true true And true is evaluated last, giving true,Tutorial 4,26,10 5 + 1 5 + 1 is evaluated first, giving 6 10 6 is evaluated third,
11、giving false true And false is evaluated last, giving false,Tutorial 4,27,Expression Containing the Or Logical Operator,8 = 4 * 2 Or 7 5 is evaluated third, giving false true Or false is evaluated last, giving true,Tutorial 4,28,学生成绩存放在sngScore,写出表达式学生成绩在60分和70分之间 sngScore=60 and sngScore60 or sngSc
12、ore70,Tutorial 4,29,二、If语句,单行结构IfThen只能依据一个条件的值进行判断 块结构IfThenElse能依据多个条件进行判断 条件嵌套,Tutorial 4,30,1.单行结构IfThen,执行单语句 执行语句块,Tutorial 4,31,1)执行单语句,语法:If 条件表达式 Then 语句1 必须写在一行或加空格和_强制分行 程序执行到此结构时,先计算条件表达式的值,其值只有True/False两个。若值为True,则执行语句1;值为False,跳过此结构,直接执行If结构后面的语句。,Tutorial 4,32,例1,Private Sub Command1
13、_Click() Label2.Caption = 错误! If Text1.Text = 1 Then label2.Caption = 正确! End Sub 能否将两条语句顺序反过来?,Tutorial 4,33,2)执行语句块,If 条件表达式 Then 语句1 语句2 语句n End If,Tutorial 4,34,例2 限制输入,Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii 57 Then MsgBox (输入数据非法) KeyAscii = 0 End If End Sub,Tutorial 4,35,2
14、.块结构IfThenElse,块结构使程序能够依据多个条件值执行语句,其语法格式如下: If 条件表达式1 Then 语句块1 ElseIf 条件表达式2 Then 语句块2 Else 语句块n End If,Tutorial 4,36,块结构执行的过程是:首先测试条件表达式1,如果为True,程序就执行语句块1。如果为 False,程序就测试条件表达式2,依次类推,直到找到一个为 True 的条件。当它找到一个为 True的条件时,程序就会执行相应的语句块,然后跳出块结构,执行 End If后面的代码。 块结构中可以包含 Else 语句块,也可以不包含。Else 语句块的作用是:当程序测试到
15、所有的条件表达式的值都为False时,则执行 Else 语句块。 块结构中的语句块,可以是一条语句,也可以是多条语句。,Tutorial 4,37,If sngScore = 85 Then Print 优秀 ElseIf sngScore = 70 Then Print 中等 ElseIf sngScore = 60 Then Print 合格 ElseIf sngScore = 0 Then Form1.ForeColor = vbRed Print 不及格 Else Print 成绩输入错误! End If,Tutorial 4,38,3. 条件嵌套,程序设计时可能会遇到这种情况,当进行
16、条件判断后执行某个分支时,需要再次进行新的条件判断,从而执行下一个分支。程序的结构有如树形。这时可以采用条件嵌套的方式,即在If结构内部某个语句块中再次加入If结构。,Tutorial 4,39,例3 If txtPassWord.Text = 1234 Then Unload frmPassWord frmSystem.Show Else MsgBox 密码错误! Count = Count + 1 If (Count = 3) Then txtPassWord.Enabled = False cmdIn.Enabled = False End If End If,Tutorial 4,40
17、,Nested If Example 1,Write a selection structure that assigns a sales tax rate to the sngTax variable. The tax rate is determined by the state code stored in the intCode variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate,Tutorial 4,4
18、1,Nested If Example 1,If intCode = 1 Or intCode = 3 Then sngTax = .04 Else If intCode = 2 Then sngTax = .05 Else sngTax = .02 End If End If,Tutorial 4,42,Nested If Example 2,Write a selection structure that assigns a bonus to the sngBonus variable. The bonus is determined by the salespersons code (i
19、ntCode) and, in some cases, by the sales amount (sngSales). If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople re
20、ceive $550. All others receive $150.,Tutorial 4,43,Nested If Example 2,If intCode = 1 Then If sngSales = 10000 Then sngBonus = 500 Else sngBonus = 200 End If Else If intCode = 2 Then If sngSales = 20000 Then sngBonus = 600 Else sngBonus = 550 End If Else sngBonus = 150 End If End If,Tutorial 4,44,Ne
21、sted If Example 2,If intCode = 1 And sngSales = 10000 Then sngBonus = 500 Else If intCode = 1 And sngSales = 20000 Then sngBonus = 600 Else If intCode = 2 And sngSales 20000 Then sngBonus = 550 Else sngBonus = 150 End If End If End If End If,Tutorial 4,45,三、Select Case语句,Select Case 语句的能力与 If.Then.E
22、lse 语句类似,但对多重选择的情况,Select Case 语句使代码更加易读。 Select Case 在结构的上方处理一个测试表达式并只计算一次,然后将表达式的值与结构中的每个 Case 的值进行比较。如果相等,就执行与该 Case 相关联的语句块。,Tutorial 4,46,Select Case 测试表达式 Case 表达式值1 语句块1 Case 表达式值2 语句块2 . Case Else 语句块n End Select,Tutorial 4,47,Select Case 表达式值有如下规定:,可以是一个值,也可以是几个值的列表。 如果列表中的值不连续,就用逗号把值隔开。如:C
23、ase 1,3,7,10。 如果列表中的值是连续的,可用To表达式,如Case 1 To 5。表示1到5这5个数。 列表中还可以包含Is关系表达式,如Case Is79。表示大于79的值。 表达式值的类型必须与测试表达式的类型一致。,Tutorial 4,48,Select Case sngScore Case 85 To 100 Print 优秀 Case 70 To 85 Print 中等 Case 60 To 70 Print 合格 Case 0 To 60 Form1.ForeColor = vbRed Print 不及格 Case Else Print 成绩输入错误! End Select,Tut
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 48217-2026温室气体产品碳足迹量化方法与要求电动汽车
- GB/T 48261-2026地理标志产品质量要求保山小粒咖啡
- 电子商务师考试电子商务创新与创业管理习题集及答案
- 2026年电子商务师职业资格考试电子商务跨境电商政策法规与风险管理专项训练习题集及答案
- 2026年公共卫生管理类资格考试卫生毒理学备考习题及答案
- 2026年安全评价师安全管理实务操作模拟试卷及答案
- 安全生产管理人员安全生产法律法规专项习题集及答案
- 2026年统计师考试统计咨询实务专项训练题库及答案
- 助产资格证考试助产专业临床护理技能操作模拟试题及答案
- 上海市低压电工操作证考试电气设备安装与调试模拟试题及答案
- 河南省开封市2026届高三下学期第二次质量检测数学试题(解析版)
- 2026年消防月活动主题宣贯课件
- 涂装废气RCO治理设备运维管理手册
- 接触器更换方法
- 医疗废物岗前培训
- 城市更新项目中的BIM技术应用
- 幼儿园大班科学活动《有用的工具》课件
- 新22J01 工程做法图集
- 旧桥拆除安全培训课件
- 美术课件《动漫人物中手的画法》
- DLT 1051-2019电力技术监督导则
评论
0/150
提交评论