An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
非零为True 零为False 逻辑运算符 • 逻辑运算符可以对数值、表达式进行比较 • 逻辑运算的结果一般为逻辑值, 还可以是数值 Not 3 > 5 ‘true 3 > 5 And 4 >= 2 ‘false Not 3 > 5 Or 3 + 2 < 4 ‘true a <= x And x <= b ‘表示a≤x≤b 表示数学区间〔a,b〕内的x: a <= x And x <= b
00000000 00001100 And 00000000 00000111 00000000 00000100 00000000 00001100 Or 11111111 11111001 11111111 11111101 逻辑运算符 • 12 And 7 运算结果为:4 • 12 Or -7 运算结果为:-3 逻辑值参与逻辑运算,结果为逻辑值 数值数据参与逻辑运算,结果为数值(按位操作)
运算符的优先级 算术运算符>字符串运算符>关系运算符>逻辑运算符 & ^ -(负号) *, / \ Mod +,- 无 优 先 顺 序 Not And Or Xor 由高到低 例:设a=4,b=3,c=2,d=1, 表达式a>b+1Or c<d And b Mod c 的值 ?
开始 dim max as single dim min as single max=val(text1.text) min=val(text2.text) F max<min? T max=min Label3.Caption = "较大值为" & max 结束 If语句 • 任务1 输入两个数,输出较大的数。 • 编写代码: Private Sub Command1_Click() Dim max As Single Dim min As Single max = Val(Text1.Text) min = Val(Text2.Text) If max < min Then max = min End If Label3.Caption = "较大值为" & max End Sub Dim max As Single Dim min As Single max = Val(Text1.Text) min = Val(Text2.Text) If max < min Then max = min Label3.Caption = "较大值为" & max If max < min Then max = min 注意: • 块if语句中的end if能否省略? 注意: • 块if语句中的end if能否省略? • 将块if语句改为单行if语句怎么改?
T F 用户名和口 令正确吗? 显示"欢迎" 消息框 显示"错误" 消息框 If语句 • 任务2 编写一个程序,实现当用户输入的用户名和口令都正确时显示"欢迎登录"的消息对话框,当用户名或口令有错误时显示消息对话框报告错误。(假设用户名为SYL,密码为“123456”)
If语句 • 任务2 编写一个程序,实现当用户输入的用户名和口令都正确时显示"欢迎登录"的消息对话框,当用户名或口令有错误时显示消息对话框报告错误。 • 编写代码: • Private Sub Command1_Click() • Dim user As String • Dim psw As String • user = Text1.Text • psw = Text2.Text • End Sub Ifuser ="SYL" And psw = "123456" Then MsgBox ("欢迎您登录本系统!") Else MsgBox ("您的用户名或口令有误,请重新输入!") End If
If ElseIf 不被4整除 F ElseIf F 不被100整除 T 被400整除 Else F T Then Then T Then 不是闰年 是闰年 是闰年 不是闰年 课堂练习 • 判断某一年是否是闰年。闰年的条件是符合下面二者之一:①能被4整除,但不能被100整除; ②能被400整除
练习 Dim Year%, leap As Boolean Year = Val(InputBox("Input year")) If Year Mod 4 <> 0Then leap = False ElseIfYear Mod 100 <> 0Then leap = True ElseIf Year Mod 400 = 0Then leap = True Else leap = False End If
IIf( )函数 • 变量=IIf( 表达式, 当条件为真的值, 当条件为假的值) Dim intX%, intY%, intMax% intX= 15 intY = 10 intMax = IIf( intX > intY, intX , intY) Print intMax If intX > intY Then intMax = intX Else intMax = intY End If
IIf( )函数 IIf(条件, 表达式1, 表达式2) If user ="SYL" And psw = "123456" Then MsgBox ("正确!") Else MsgBox ("错误!") End If MsgBox IIf(user = "SYL" And psw = "123456", "正确!", "错误!") IIf(user = "SYL" And psw = "123456", MsgBox("正确!"),MsgBox("错误!"))
If语句 T • 编写代码: • Private Sub Command1_Click() • Dim score As Single • Dim s As String • score = Val(Text1.Text) • End Sub If score < 60 Then s = "不及格" ElseIf score < 70 Then s = "及格" ElseIf score < 80 Then s = "中等" ElseIf score < 90 Then s = "良好" Else s = "优" End If Text2.Text = s
If语句 T • 任务4 学校对学习成绩优良的同学给予奖励。 • 编写代码: • Private Sub Command1_Click() • Dim a As Single, b As Single, c As Single • a = Val(Text1.Text):b = Val(Text2.Text):c = Val(Text3.Text) • If a + b + c >= 285 Then • Label4.Caption = "可以获奖!" • ElseIf a >= 90 And b >= 90 And c >= 90 Then • Label4.Caption = "可以获奖!" • ElseIf a = 100 And b = 100 And c >= 80 Then • Label4.Caption = "可以获奖!" • Else • Label4.Caption = "不可获奖!" • End If • End Sub 能改成下面的代码吗? If a + b + c >= 285 or a >= 90 And b >= 90 And c >= 90 or _ a = 100 And b = 100 And c >= 80 Then Label4.Caption = "不可获奖!" End If
IF语句的嵌套 If a>100 then If b>100 then Print “a,b均大于100” endif Endif
【例】征兵条件:男性18~20岁,1.65米以上;女性16~18岁,1.60米以上,要求:从键盘输入一个人信息,判断是否符号条件【例】征兵条件:男性18~20岁,1.65米以上;女性16~18岁,1.60米以上,要求:从键盘输入一个人信息,判断是否符号条件 If 性别 = “男” Then ‘为男性 If 年龄 >= 18 And 年龄 <= 20 Then If 身高 >= 1.65 Then Print "合格" End If End If Else ‘为女性 If 年龄 >= 16 And 年龄 <= 18 Then If 身高 >= 1.6 Then Print "合格" End If End If End If 例题
IF语句的嵌套 • 比较x,y,z的大小,并进行调整,使得x>y>z • 效果: X > Y > Z • X Y if(x<y) 交换x,y • X > Y Z
y<z False True 交换y和z x<y False True 交换x和y IF语句的嵌套 比较x,y,z的大小,并进行调整 使得x>y>z If x < y Then t = x: x = y: y = t EndIf If y<z Then t = y: y = z: z = t If x < y Then t = x: x = y: y = t End If End If
Label4. Caption= " " Label4. Caption= " " a ^ 2 = b ^ 2 + c ^ 2 Or b ^ 2 = a ^ 2 + c ^ 2 Or c ^ 2 = a ^ 2 + b ^ 2 a + b > c And a + c > b And b + c > a Label4. Caption= " " T F 三角形? T F 输出判断结果 直角三角形? 输出判断结果 输出判断结果 If语句 T • 任务5 编写一个程序,让用户输入三个数判并断该三个数能否组成一个直角三角形。 • 编写代码: • Private Sub Command1_Click() • Dim a, b, c As Single • a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text) • End Sub If a + b > c And a + c > b And b + c > a Then If a ^ 2 = b ^ 2 + c ^ 2 Or b ^ 2 = a ^ 2 + c ^ 2 Or c ^ 2 = a ^ 2 + b ^ 2 Then Label4. Caption= "可以构造一个直角三角形 " Else Label4. Caption= "可以构造一个非直角三角形" End If Else Label4. Caption= "不可以构造一个三角形 " End If
小结 • 单行结构:If <条件>Then <语句组> • 块结构: If <条件> Then <语句组> End If
表达式 F T 语句组1 语句组2 双分支条件语句 • 块结构: If <条件> Then <语句组1> Else <语句组2> End If • 单行结构: If <条件> Then <语句1> Else <语句2>
If 条件1 F ElseIf 条件2 F ElseIf T 条件n F Else T Then Then T Then 语句块1 语句块2 语句块n 语句块n+1 多分支条件语句——块IF语句
If嵌套 • If <条件1> Then If <条件2> Then … If <条件n> Then <语句组n> End If … <语句组2> End If <语句组1> End If