5
This presentation is the property of its rightful owner.
Sponsored Links
1 / 46

第5章 PowerPoint PPT Presentation


  • 141 Views
  • Uploaded on
  • Presentation posted in: General

第5章. 选择结构 程序设计. 本章要点: 条件语句 单行结构条件语句 If...Then...Else ... 块结构条件语句 If...Then…EndIf 多分支选择语句 Select Case...End Select 语句的嵌套. 特点: 根据条件是否成立,决定程序的执行分支. 引言. 顺序结构程序的执行次序是按语句的先后排列次序依次执行的,然而,计算机在处理实际问题时,往往需要根据条件是否成立,决定程序的执行方向,在不同的条件下,进行不同的处理。假如遇到这样一个问题:. |x| x≤0 lnx x>0. Y=.

Download Presentation

第5章

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


5

5

  • :

  • If...Then...Else ...

  • If...ThenEndIf

  • Select Case...End Select

:


5

|x| x0

lnx x>0

Y=

xx


5

T

F

1

2

5.1

  • 1.

  • If<>Then [<1>] [Else<2>]

  • 2.

  • <>(True)<1>

  • <2>

If x <= 0 Then y = abs(x) Else y = log(x)


5

  • 3.

  • (1) <>

  • ;

  • If x > 0 Then y = 1 Else y = -1

  • If x > 0 And x < 10 Then y = 1 Else y = -1

  • 0False0True

  • x = 5

  • If x Then y = 1 Else y = -1

  • "0"False"0"True

  • (2) Else,<1>

  • If X<>"aaa" Then Print X


5

  • (3) <1><2>:

  • :

  • If N>0 Then A=A+B:B=B+A Else A=A-B:B=B-A

  • (4)

  • :If N>0 Then A=A+B:B=B+A Else_

  • A=A-B:B=B-A


5

:

  • (5)

X = 2

If X >= 0 Then X = 1 + X Else X = 5 - X

Y = 1 - X

Print "Y="; Y

X = -2

If X >= 0 Then X = 1 + X Else X = 5 - X

Y = 1 - X

Print "Y="; Y

(6)

IF ... THEN...IF ... THEN ...ELSE ...ELSE


5

Text1

Label3

Command1

  • 5-1xYy

|x| x0

lnx x>0

y=


5

Private Sub Command1_Click()

X = Val(Text1.Text)

If X <= 0 Then Y = Abs(X) Else Y = Log(X)

Label3.Caption = Y

End Sub


5

Y

X>0,Y>0

X<0,Y>0

X

X<0,Y<0

X>0,Y<0

  • 5-24:

X=0Y=0


5

Private Sub Command1_Click()

X = Val(Text1.Text)

Y = Val(Text2.Text)

If X = 0 Or Y = 0 Then _

Print "": Exit Sub

IfX > 0 ThenIf Y > 0 Then Print "IN A" _

Else Print "IN D"ElseIf Y > 0 Then _

Print "IN B" Else Print "IN C"

End Sub


5

5.2

  • If x > 10 Then

  • y = x - 10

  • ElseIf x > 5 Then

  • y = x - 5

  • ElseIf x > 0 Then

  • y = x

  • Else

  • Print "error"

  • End If

  • 1.

  • If <1>Then

  • [<1>]

  • [ElseIf <2>Then

  • [<2>]]

  • [ElseIf <N> Then

  • [<N>]]

  • [Else

  • [<>]]

  • End If


5

T

F

1

T

F

2

1

2

T

F

n

n

n+1

2.


5

  • 3.

  • IfEnd If

  • IfEnd IfElseIfElse

  • :

If <> Then

<1>

Else

<2>

End If

If <> Then

<>

End If


5

5-35-2

Private Sub Command1_Click()

X = Val(Text1.Text) : Y = Val(Text2.Text)

If X = 0 Or Y = 0 Then

Print ""

ElseIf X > 0 And Y > 0 Then

Print ""

ElseIf X < 0 And Y > 0 Then

Print ""

ElseIf X < 0 And Y < 0 Then

Print ""

Else

Print ""

End If

End Sub


5

  • 5-4FC=5/9*(F-32)CC

C>40"HOT "

30<C40" WARM "

20<C30" ROOM TEMPERATURE "

10<C20" COOL "

0<C10" COLD "

C0" FREEZING"


5

Text1

Text2


5

  • Private Sub Command1_Click()

  • C = 5 / 9 * (Val(Text1.Text) - 32)

  • Text2.Text = Format(C, "0.00")

  • If C > 40 Then

  • Label2.Caption = "HOT"

  • ElseIf C >30 Then

  • Label2.Caption = "WARM"

  • ElseIf C >20 Then

  • Label2.Caption = "ROOM TEMPERATURE"

  • ElseIf C >10 Then

  • Label2.Caption = "COOL"

  • ElseIf C >0 Then

  • Label2.Caption = "COLD"

  • Else

  • Label2.Caption = "FREEZING"

  • End If

  • End Sub


5

  • 4.

  • 1) ElseIfElse If

  • 2) IfElseIfElseEnd If

  • 3)

  • If x >= 0 Then

  • y = 1

  • Else

  • y = 2

  • End If

If x>=0 Then y=1

Else y = 2

End If

If x=1 Then y=10 Else y = 20

End If


5

5.3

2.

,Case,,Case,End Select

  • 1.

  • Select Case <>

  • Case <1>

  • [<1>]

  • [Case <2>

  • [<2>]]

  • [Case Else

  • [<n>]]

  • End Select


5

1

F

T

1

2

F

T

2

n

F

T

n

n+1

:


5

  • 3.

  • 1) <> :

  • 2) Case <>:

  • 1[,2]...

  • : Case 1 , 3 , 5

  • 1 TO 2

  • : Case 10 TO 30

  • Case "A" TO "Z"

  • Is <><>

  • : Case Is >= 10

  • Case Is = 0 Case 0

  • ,:

  • Case Is < -5 , 0 , 5 To 100


5

Text1

Text2

Command1

5-5y

ex+e-x x<0

1.25 x=0

lgx x>0

Y=

Text1xCommand1YText2


5

Command1Click

Private Sub Command1_Click()

X = Val(Text1.Text) ' X

Select Case X ' <>X

Case Is < 0

Y = Exp(X) + Exp(-X)

Case 0' Case Is=0

Y = 1.25

Case Is > 0

Y = Log(X) / Log(10)

End Select

Text2.Text = Format(Y, "0.000000")

End Sub


5

4.

1) <>Case

2) Case<>

Select Case X

Case X < 0 ' X

Y = Exp(X) + Exp(-X)

Case X=0 ' X

Y = 1.25

Case X > 0 ' X

Y = Log(X) / Log(10)

End Select


5

3) <>X1X2X3

Select Case X1,X2,X3 '

Case X1+X2+X3 < 0

End Select

Select Case X1+X2+X3'

Case Is < 0

End Select


5

4) Case

0<X<100

Select Case X

Case Is>0 And Is<100 ' And,

End Select

Case


5

5-6:

250km

250km500km 2%

500km1000km 5%

1000km2000km 8%

2000km3000km 10%

3000km 15%


5

Price

Weight

Distance

Discount

Freight:

Freight= Price * Weight * Distance *

(1 - Discount)


5

Text1

Text2

Text3

Text4

Command1


5

Command1Click:

Private Sub Command1_Click()

Price = Val(Text1.Text)

Weight = Val(Text2.Text)

Distance = Val(Text3.Text)

Select Case Distance

Case Is < 250

Discount = 0

Case Is < 500

Discount = 0.02

Case Is < 1000

Discount = 0.05


5

Case Is < 2000

Discount = 0.08

Case Is < 3000

Discount = 0.1

Case Else

Discount = 0.15

End Select

Freight = Price * Weight * Distance * _

(1 - Discount)

Text4.Text = Freight

End Sub


5

5.5


5

113578101231469113022928

24100400


5

Text1

Text2

Text3


5

Private Sub Command1_Click()

Dim Ynum As Integer, Mnum As Integer

Ynum = Val(Text1.Text)

Mnum = Val(Text2.Text)

Select Case Mnum

Case 1, 3, 5, 7, 8, 10, 12

Text3.Text = 31

Case 4, 6, 9, 11

Text3.Text = 30


5

Case 2

If (Ynum Mod 4 = 0 And_

Ynum Mod 100 <> 0) Or _

(Ynum Mod 400 = 0) Then

Text3.Text = 29

Else

Text3.Text = 28

End If

End Select

End Sub


5

5.6

  • 5-8ax2+bx+c=0

  • :

  • 1) a=0b=0b 0: x=-c/b

  • 2) a 0b2-4ac=0

  • 3) a 0b2-4ac>0

  • 4) a 0b2-4ac<0


5

Text1

Text2

Command1

Text3


5

  • Private Sub Command1_Click()

  • A = Val(Text1.Text): B = Val(Text2.Text)

  • C = Val(Text3.Text): Cls

  • CurrentX = 600: CurrentY = 1100

  • If A = 0 Then

  • If B = 0 Then

  • MsgBox ""

  • Text1.SetFocus

  • Text1.SelStart = 0

  • Text1.SelLength = Len(Text1.Text)

  • Else

  • X = -C / B

  • Print "X="; Format(X, "0.000")

  • End If

  • Exit Sub '

  • End If


5

  • Delta = B ^ 2 - 4 * A * C

  • Select Case Delta

  • Case 0

  • Print "X1=X2="; Format(-B / (2 * A), "0.000")

  • Case Is > 0

  • X1 = (-B + Sqr(Delta)) / (2 * A)

  • X2 = (-B - Sqr(Delta)) / (2 * A)

  • Print "X1="; Format(X1, "0.000")

  • CurrentX = 600: CurrentY = 1300

  • Print "X2="; Format(X2, "0.000")

  • Case Is < 0

  • A1 = -B / (2 * A)

  • A2 = Sqr(Abs(Delta)) / (2 * A)


5

  • Print "X1="; Format(A1, "0.000"); "+"; _

  • Format(A2, "0.000"); "i"

  • CurrentX = 600

  • CurrentY = 1300

  • Print "X2="; Format(A1, "0.000"); "-"; _

  • Format(A2, "0.000"); "i"

  • End Select

  • End Sub


5

Text1

Label2

  • 5-9,


5

  • Text1Text1

  • PasswordChar*

  • MaxLength6

  • Text1KeyUpKeyUpKeyCodeKeyCode13Text1

  • Text1KeyUp


5

  • Private Sub Text1_KeyUp(KeyCode As Integer, _ Shift As Integer)

  • Static I As Integer'

  • If KeyCode = 13 Then'

  • IfUCase(Text1.Text) = "HELLO" Then

  • Label2.Caption = "!,"

  • ElseIf I = 0 Or I = 1 Then

  • I = I + 1

  • Label2.Caption = "!,"

  • Text1.SelStart = 0

  • Text1.SelLength = Len(Text1.Text)

  • Else

  • MsgBox ",": End

  • End If

  • End If

  • End Sub


5

1.

If <> Then [<1>] [Else <2>]

2.

If <1> Then

[<1>]

[ElseIf <2> Then

[<2>]]

[ElseIf <n> Then

[<n>]]

[Else

[<n+1>]]

End If


5

3.

  • Select Case <>

  • Case <1>

  • [<1>]

  • [Case <2>

  • [<2>]]

  • [Case <n>

  • [<n>]]

  • [Case Else

  • [<n+1>]]

  • End Select


  • Login