1 / 32

3. Flow Control ( 流程控制 )

3. Flow Control ( 流程控制 ). If … Then … else. condition 通常是一個布林 (Boolean) 運算式 一個為零的數值會被視為 False ,而任何非零數值都被看作 True 。 若 condition 為 True ,則 Visual Basic 會執行 Then 關鍵字後面的所有指令碼 (statements) ;反之,則執行 Else 後面的指令碼 (statements) 。. Example ( 範例 ). If … then … else. ' 要求使用者輸入

tiva
Télécharger la présentation

3. Flow Control ( 流程控制 )

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.

E N D

Presentation Transcript


  1. 3. Flow Control (流程控制)

  2. If…Then…else • condition通常是一個布林(Boolean)運算式 • 一個為零的數值會被視為False,而任何非零數值都被看作 True。 • 若 condition為 True,則 Visual Basic 會執行 Then 關鍵字後面的所有指令碼(statements);反之,則執行Else後面的指令碼(statements)。

  3. Example (範例)

  4. If…then…else '要求使用者輸入 0004: Console.Write("請輸入一個數字:") 0005: number = Console.ReadLine 0006: '判斷 0007: If number < 10 Then 0008: digits = 1 0009: ElseIf number < 100 Then 0010: digits = 2 0011: ElseIf number < 1000 Then 0012: digits = 3 0013: ElseIf number < 10000 Then 0014: digits = 4 0015: Else 0016: digits = 5 0017: End If '判斷位數 0019: If digits = 1 Then Result = "1位數" 0020: If digits = 2 Then Result = "2位數" 0021: If digits = 3 Then Result = "3位數" 0022: If digits = 4 Then Result = "4位數" 0023: If digits = 5 Then Result = "大於位數" 0024: '顯示 0025: Console.Write("數字:" & number & " --> " & Result)

  5. In-Class Exercise • 請讓使用者輸入兩個數字,並且透過選擇,計算兩個數字之+、-、*、之值。

  6. Select…Case (條件判斷敘述 ) • 如果 test expression符合任何 Case expression 子句,即會執行接在該 Case後的陳述式(statements),直到下一個Case關鍵字或是End Select陳述式。 • 如果 test expression 符合一個以上的 Case 子句,則程式只會執行接在第一個符合條件者之後的陳述式 • 如果在每一個Case 陳述式之間找不到符合條件者,則Case Else陳述式後面的程式碼會被執行。

  7. Select…Case

  8. Select …Case

  9. Example (範例) 讓使用者輸入月份,透過Select Case判斷屬於哪一種季節

  10. Pitfall (陷阱) 必須由小而大 執行後跳離Select Case

  11. In-Class Exercise • 請讓使用者輸入兩個數字,並且透過選擇,計算兩個數字之+、-、*、之值。

  12. Nested loop (巢狀迴圈)

  13. In-Class Exercise • 以下表格為虛擬之電價計價方式。請撰寫一VB程式,讓使用者輸入用電月份、用電度數、以及住家或者營業用電,然後計算出所應繳的電費。

  14. For … Next • For和Next兩個關鍵字中的程式碼,會被重複的執行 • 執行的次數會依照變數counter來決定 • Counter將會從start開始到end,每次遞增 • Exit for: 中途離開for迴圈

  15. Example (範例) 被重複執行12次,每次變數i的值都有所不同

  16. Nested For loop (巢狀for迴圈) 巢狀迴圈

  17. Dim i, j As Integer For i = 1 To 10 For j = i To 10 Console.Write("*") Next Console.WriteLine() Next Console.ReadKey()

  18. Dim i, j As Integer For i = 1 To 10 For j = i To 10 Console.Write("*") Next Console.WriteLine() Next Console.ReadKey()

  19. Dim i, j As Integer For i = 1 To 10 For j = 1 To i Console.Write("*") Next Console.WriteLine() Next For i = 1 To 10 For j = i To 10 Console.Write("*") Next Console.WriteLine() Next Console.ReadKey()

  20. Dim i, j As Integer For i = 1 To 10 For j = i To 10 Console.Write(" ") Next For j = 1 To i Console.Write("*") Next Console.WriteLine() Next Console.ReadKey()

  21. Dim i, j As Integer For i = 1 To 10 For j = i To 10 Console.Write(" ") Next For j = 1 To i Console.Write("*") Next Console.WriteLine() Next For i = 1 To 10 For j = i To 10 Console.Write("*") Next Console.WriteLine() Next Console.ReadKey()

  22. In-Class Exercise • 請撰寫一VB程式,列出九九乘法表

  23. Exit For 利用Exit For允許中途離席(請注意,Exit For一定會出現在判斷式之後)

  24. Do…While condition是判斷條件,當程式碼進入Do…Loop迴圈之後,會一直執行,每次執行時都會判斷condition條件是否成立,但是依照語法的不同,有幾種不同的可能…

  25. 利用Do…While確認密碼 Sub Main() Dim id, pwd As String Do Console.Write("請輸入登入帳號:") id = Console.ReadLine Console.Write("請輸入密碼:") pwd = Console.ReadLine Loop Until id = "must" And pwd = "12345" Console.WriteLine("歡迎登入...") Console.ReadKey() End Sub

  26. Sub Main() Dim n As Integer Dim k As Integer = 0 Console.Write("請輸入一個數字:") n = Console.ReadLine Do k = k + n Loop Until k + n > 10000 Console.WriteLine("小於10000, 最接近10000的" & n & "倍數為:" & k) Console.ReadKey() k = 0 Do k = k + n Loop While k + n < 10000 Console.WriteLine("小於10000, 最接近10000的" & n & "倍數為:" & k) Console.ReadKey() k = 0 Do While k + n < 10000 k = k + n Loop Console.WriteLine("小於10000, 最接近10000的" & n & "倍數為:" & k) Console.ReadKey() k = 0 Do Until k + n > 10000 k = k + n Loop Console.WriteLine("小於10000, 最接近10000的" & n & "倍數為:" & k) Console.ReadKey() End Sub

  27. 四種 Do … While

  28. Example (範例) 程式會讓使用者輸入一個數字並找到這個數字最接近10000的倍數(最接近10000,但不能超過)

  29. While … End While While condition [statements] [Exit While] [statements] End While • While … end while可以被do while … loop完全取代

  30. Infinite Loop (無窮迴圈) • 無窮迴圈編譯時沒有錯誤訊息 • 應該確保程式有程式出口 Dim i as Integer = 10 Do While i> 9 i=i+1 Loop

  31. Try…Catch 錯誤處理

  32. Example Sub Main() Dim i As Integer = 10 Try Do While i > 9 i = i + 1 Loop Catch ex As Exception Console.WriteLine("程式發生問題:" & ex.Message) Console.ReadKey() Exit Sub End Try End Sub

More Related