第五章 選擇結構

Download 第五章     選擇結構

Post on 31-Dec-2015

28 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

第五章 選擇結構. 本章投影片僅供本書上課 教師 使用 , 非經同意請勿拷貝或轉載. 5-1 關係運算式與邏輯運算式. 關係運算式 和 邏輯運算式 是構成條件基本要件。運算式經過運算產生 True( 真 ) 或 False( 假 )  供我們決定程式執行的流向。 一、關係運算式 關係運算式功能  用來比較數值、字串等資料的大小關係。. 關係運算子種類與關係運算式用法:. 【 例 1】 7 > 4  結果 True 【 例 2】 "a" > "b"  結果為 False - PowerPoint PPT Presentation

TRANSCRIPT

  • 5-1 True() False()

  • 1 7 > 4 True

    2 "a" > "b" False a ASCII97 b ASCII98

    3#3/17/2006# >= #1/1/2006# True

    4(tall)186 tall

  • AB

    And A And BAB Or A Or B AB AndAlso A AndAlso BAnd AFalseB OrElse A OrElse BOr ATrueB Not Not AA Xor A Xor BAB

  • 1 (score)92(grade) (score >=90) And (grade= "")2 60 score < 80 (score>=60) And (score
  • 5-2 (algorithm) 1. 2. 3. 4. 5.

  • 1. Pseudo Code: 2. (Flow Chart)

  • (Pseudo Code)

  • (Flow Chart)

  • 1. Step1 (sum) = 0 Step2 (score) Step3 score >= 0 Step 6 Step4 sum = sum + score Step5 Step 2 Step6 sum Step7

  • 2.

  • 5-3 1. 2. 3.

  • .

  • :1.

  • 2.

  • 3.

  • . (Loop) 1. 2.

  • A

  • 5-4

  • . If ThenIf Then End If

    If (a < 0) Then a = -a ' End If

    If (score < 60) Then 60 sum += score ' count+=1 ' 1 count0End If

  • If Then Else If ThenAElseBEnd If

  • sex = "" ",! " sex = "" ", ! " Dim sex As String = "" If sex = "" Then Label1.Text = ", !" Else Label1.Text = ", !" End If

  • IIf IIf If Then Else

  • (score) 60 ""

    1. If Then Else

    If score > =60 Then MsgBox("") Else MsgBox("") End If

  • 2. IIf MsgBox ( IIf (score >=60, "", "" ) ) IIf amount 1000 amount 5000 Label1 Label1.Text = amount * ( IIf ( amount >= 5000, 0.8, IIf(amount >= 1000, 0.9,1 )))

  • IfIIF(score)(grade) 0 score
  • Label1Label2

  • IfSelect Case ; IfIIFIf70 :70IIF(score>80, "A", "B") "A""B" 70IIF(score>60, "C", "D") "C""D" (btnOk)btnOk_Click (btnEnd)btnEnd_ClickEnd

  • 1. : "grade" (P.30)2. :

    NameTextFont/Size Label1 () 12 Label2 () :12 Label3 lblShow :12 TextBox1 txtScore 12 Button1 btnOk 12 Button2 btnEnd 12

  • ' FileName :grade.sln01 Private Sub btnOk_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOk.Click02 Dim score As Integer03 score = Val(txtScore.Text)04 If (score >= 70) Then >=705705 lblShow.Text = " : " & IIf(score >= 80, "A", "B")06 Else 5: IIF>=80AB07 lblShow.Text = " : " & IIf(score >= 60, "C", "D")08 End If 7: IIF>=60CD09 End Sub10 ' -------------------------------------------------------------------------------------11 Private Sub btnEnd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnEnd.Click12 End13 End Sub

  • 5-5 ;

  • (score)(grade) 0 score
  • 80

  • ' FileName :NestIf.sln01 Private Sub btnOK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOK.Click02 Dim score As Integer03 score = Val(txtScore.Text)04 If (score < 0 Or score > 100) Then05 MsgBox(" 0~100 ")06 txtScore.Text = ""06 Else07 If (score >= 70) Then08 If (score >= 80) Then09 lblShow.Text = " : A "10 Else11 lblShow.Text = " : B "12 End If

  • 13 Else14 If (score >= 60) Then15 lblShow.Text = " : C "16 Else17 lblShow.Text = " : D "18 End If19 End If20 End If21 End Sub22 ----------------------------------------------------------------------------------23 Private Sub btnEnd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnEnd.Click24 End25 End Sub

  • 5-6 If

  • 1. Select Case 2. Choose 3. Switch

  • Select Case() ; End Select

  • 1 Case "Y" Y2 Case "abc" "ABC"3 Case -50100 501004 Case 60 To 100601005 Case Is < 6060Case()

  • 12 InputBox

  • ' FileName :Tax.sln01 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load02 Dim income, discount, tax As Integer ',03 Dim tax_rate As Single '04 Dim msg As String05 income = Val(InputBox("", " _ ")) 'InputBox 06 msg = "" & vbNewLine07 Select Case income08 Case Is
  • 17 Case 1980001 To 372000018 tax_rate = 0.319 discount = 28330020 Case Is >= 372000121 tax_rate = 0.422 discount = 65530023 End Select24 tax = income * tax_rate = * 25 msg &= " " & Format(income, " #,###") & "" _ & vbNewLine26 msg &= " " & Format(tax_rate, "0%") & vbNewLine27 msg &= "------------------------------------------" & vbNewLine28 msg &= " " & Format(tax, "#,##0") & "" & vbNewLine29 msg &= " " & Format(discount, "#,##0") & "" & _ vbNewLine30 msg &= "------------------------------------------" & vbNewLine31 msg &= "" & Format(tax - discount, "#,##0") & ""32 MsgBox(msg) MsgBox ; 31:= 33 End34 End Sub

  • Choose1 res = Choose(i,"","","","");i=2res = "" 2 res = Choose(i,"1","one","","") i=3 res = "" i=5 res NullChoose

  • Switch1msg = Microsoft.VisualBasic.Switch(sex = "", "", _ sex = "", "") sex= "" msg= "" 2 degree=Microsoft.VisualBasic.Switch (score>=90 And score=80 And score=70 And score=60 And score=0 And score
  • 5-7 RadioButton (i.e. )CheckedTrueCheckedFalse

  • RadioButton

  • CheckedCheckedChangedClickCheckedClickCheckedChanged

  • :1. CheckedChanged Checked 2. Click

  • 1~3 RadioButtonLabel1Label2

  • 1.Text2. : msg1~msg3 count1~count3 str1~str3

  • () 3.ClickCheckedChanged1 : click CheckedChangedCheckedTrueFalseFalseTrue

  • () 4.CheckedChanged1 : IfCheckedTrue? Checked=True1lblShow Checked=False

  • () 5.Form1_Load : count1~ count30 str1~ str3 msg1() msg2() msg3()

  • 1. : radibtn ()2. :

    NameTextAutoSize Label1 Label1True RadioButton1 rdbeBook True RadioButton1rdbCloudTrue RadioButton1rdbKinect True Label2 lblShow Label2False

  • Label1

  • ' FileName :radibtn.sln01 Dim msg1, msg2, msg3 As String 02 Dim str1, str2, str3 As String 03 Dim count1, count2, count3 As Integer 04 ' --------------------------------------------------------------------------------------05 Private Sub Form1_Load(ByVal sender ) Handles Me.Load06 msg1 = "( e-book) PCPDA( iPad) "07 msg2 = "(Cloud computing) ....( Youtube facebookBlocker)"08 msg3 = "(Kinect)Kinect 2010 "09 count1 = 0 : count2 = 0 : count3 = 0 5~1310 str1 = rdbeBook.Text ()11 str2 = rdbCloud.Text ()12 str3 = rdbKinect.Text ()13 End Sub

  • 15 Private Sub rdbeBook_CheckedChanged(ByVal sender ) _ Handles rdbeBook.CheckedChanged16 Dim strMsg As String 15~2517 If rdbeBook.Checked =True then 17~24Checked18 count1 = count1 + 1 True119 strMsg = str1 & "(" & Str(count1) & " ) "20 rdbeBook.Text = strMsg21 lblShow.BackColor = Color.Aqua22 lblShow.Font = New Font("" , 14 , FontStyle.Regular)23 lblShow.Text = rdbeBook.Text & vbCrLf & msg124 End If 23msg1()lblshow25 End Sub

  • 27 Private Sub rdbCloud_CheckedChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ rdbCloud.CheckedChanged28 Dim strMsg As String 27~3729 If rdbCloud.Checked = True Then30 count2 = count2 + 131 strMsg = str2 & "(" & Str(count2) & " )"32 rdbCloud.Text = strMsg33 lblShow.BackColor = Color.Yellow34 lblShow.Font = New Font("", 14, FontStyle.Regular)35 lblShow.Text = rdbCloud.Text & vbCrLf & msg236 End If37 End Sub

  • 39 Private Sub rdbKinect_CheckedChanged(ByVal sender _ As System.Object, ByVal e As System.EventArgs) _ Handles rdbKinect.CheckedChanged40 Dim strMsg As String 39~4941 If rdbKinect.Checked = True Then42 count3 = count3 + 143 strMsg = str3 & "(" & Str(count3) & " )"44 rdbKinect.Text = strMsg45 lblShow.BackColor = Color.LightPink46 lblShow.Font = New Font("", 14, FontStyle.Regular)47 lblShow.Text = rdbKinect.Text & vbCrLf & msg348 End If49 End Sub

  • 5-8 CheckBox(RadioButton)(CheckBox) (i.e. )

  • 5-8 CheckBox()

  • ()

  • ThreeStateFalseCheckedChangedCheckStateChangedClickThreeStateTrueCheckedC