1 / 39

第 4 章 : 控制結構

第 4 章 : 控制結構. 大綱 4.1   介紹 4.2   演算法 4.3   虛擬碼 4.4   控制結構 4.5   If / Then 選擇結構 4.6   If / Then / Else Selection Structure 4.7   While 的迴圈結構 4.8   Do While / Loop 的迴圈結構 4.9   Do Until / Loop 的迴圈結構 4.10   指定運算子與算數指定運算子 4.15   視窗應用程式. 4.1 介紹. 結構化的程式 控制結構 有助於建立及操作物件.

Download Presentation

第 4 章 : 控制結構

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. 第 4章: 控制結構 大綱 4.1   介紹 4.2   演算法 4.3   虛擬碼 4.4   控制結構4.5   If/Then選擇結構4.6   If/Then/Else Selection Structure 4.7   While的迴圈結構4.8   DoWhile/Loop的迴圈結構4.9   DoUntil/Loop的迴圈結構4.10   指定運算子與算數指定運算子4.15   視窗應用程式

  2. 4.1 介紹 • 結構化的程式 • 控制結構 • 有助於建立及操作物件

  3. 4.2 演算法 • 演算法 • 解決問題的程序與步驟 • 動作執行的步驟描述,

  4. 4.3 虛擬碼 • 虛擬碼 • 非正式的語言幫助設計者發展演算法 • 不執行在電腦上 • 幫助說明程式 • 描述可執行的敘述

  5. 4.4 控制結構 • 控制轉移 • GoTo敘述 • 易引起程式非結構化使程式不易遵守 • 所有程式可被三種結構完成 • 順序結構(Sequence structure) • 選擇結構(Selection structure) • (重複結構(Repetition structure)

  6. 4.4 控制結構 • 流程圖 • 圖形化表示演算法 • 繪圖使用以下符號 • 方形-一般敘述 • 菱形-判定敘述 • Ovals-輸入輸出 • Small circles-連接

  7. add grade to total total = total + grade add 1 to counter counter = counter + 1 4.4控制結構 Fig. 4.1 流程圖 Visual Basic’s 順序控制

  8. 4.4控制結構 • 選擇結構 • If/Then • 單選結構 • If/Then/Else • 二選結構 • SelectCase • 多選結構 • Elseif多選結構

  9. 4.4控制結構 • 迴圈結構 • While • DoWhile/Loop • Do/LoopWhile • DoUntil/Loop • Do/LoopUntil • For/Next • ForEach/Next

  10. 關鍵字(keyword) Fig. 4.2 vb.net關鍵字.

  11. 關鍵字(keyword) Fig. 4.2 vb.net關鍵字

  12. 4.5 If/Then選擇結構 • 一個進入口及一個出口 • 如 If grade>= 40Then Console.WriteLine(“ok”) End If • If 1>3 then console.write(“no”) end if

  13. true Console.WriteLine(“ok”) Grade >= 40 false 4.5 If/Then選擇結構 Fig. 4.3 流程圖 Visual Basic’s 單選結構

  14. 4.6 If/Then/Else選擇結構 • Example If Grade >= 40Then Console.WriteLine(“ok”) Else Console.WriteLine(“no”) End If • 巣狀 If/Then/Else結構 • If ---Else 結構包含if ---lese結構形成多選 • ElseIf關鍵字

  15. false true Console.WriteLine(“yes”) Console.WriteLine(“no”) Grade >= 40 4.6 If/Then/Else選擇結構 Fig. 4.4 二選結構的流程圖

  16. 4.7 While 的迴圈結構 • 迴圈結構 • 依據某些條件值執行迴圈結構的重複敘述 • 例如 (虛擬碼) While more purchasing items Purchasing next item Crossing it off my list

  17. 每一次測驗product的值 1 ' Fig. 4.5: While.vb 2 ' Demonstration of While structure. 3 4 Module modWhile 5 6 Sub Main() 7 Dim product As Integer = 2 8 9 ' structure multiplies and displays product 10 ' while product is less than 1000 11 While product <= 1000 12 Console.Write("{0} ", product) 13 product = product * 2 14 End While 15 16 Console.WriteLine() ' write a blank line 17 18 ' print result 19 Console.WriteLine("Smallest power of 2 " & _ 20 "greater than 1000 is {0}", product) 21 Console.ReadLine() ' prevents window from closing 22 End Sub ' Main 23 24 End Module' modWhile While.vb輸出 2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024

  18. true product <= 1000 product = product * 2 false 4.7 while迴圈結構 Fig. 4.6 while 迴圈結構的流程圖

  19. 4.8 Do While/Loop迴圈結構 • 與while迴圈結構一樣但可分為 a)前置正向- do while -loop b)後置正向 – do –loop while

  20. 1 ' Fig. 4.7: DoWhile.vb 2 ' Demonstration of the Do While/Loop structure. 3 4 Module modDoWhile 5 6 Sub Main() 7 Dim product As Integer =2 8 9 ' structure multiplies and displays 10 ' product while product is less than 1000 11 Do While product <= 1000 12 Console.Write("{0} ", product) 13 product = product * 2 14 Loop 15 16 Console.WriteLine() ' write a blank line 17 18 ' print result 19 Console.WriteLine("Smallest power of 2 " & _ 20 "greater than 1000 is {0}", product) 21 Console.ReadLine() ' prevent window from closing 22 End Sub 23 24 End Module' modDoWhile DoWhile.vb輸出 2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024

  21. true product <= 1000 product = product * 2 false 4.8 Do While/Loop while迴圈結構 Fig. 4.8 do while-loop 迴圈結構的流程圖

  22. 4.9 Do Until/Loop 迴圈結構 • 前置反向do until/loop –測試判定式是否為false (判定式在前) • 後置反向do –loop until-測試判定式是否為false(判定式在 後)

  23. 當product > 1000 為真時迴圈執行結束 1 ' Fig. 4.9: DoUntil.vb 2 ' Demonstration of the Do Until/Loop Structure. 3 4 Module modDoUntil 5 6 Sub Main() 7 Dim product As Integer = 2 8 9 ' find first power of 2 greater than 1000 10 Do Until product > 1000 11 Console.Write("{0} ", product) 12 product = product * 2 13 Loop 14 15 Console.WriteLine() ' write a blank line 16 17 ' print result 18 Console.WriteLine("Smallest power of 2 " & _ 19 "greater than 1000 is {0}", product) 20 End Sub ' Main 21 22 End Module' modDoUntil DoUntil.vb輸出 2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024

  24. 4.9 DoUntil/Loop迴圈結構 false product > 1000 product = product * 2 true Fig. 4.10 do until-loop 迴圈結構的流程圖

  25. 4.10 指定運算子與算數指定運算子 • binary operators • +, -, *, ^, &, / or \ 變數 =變數 運算子表示式 變數 運算子=表示式 • Example • 加法指定運算子, += value = value + 3 value += 3

  26. 4.10 算數指定運算子 Fig. 4.11 算數指定運算子

  27. 算數指定運算子 1 ' Fig. 4.12: Assignment.vb 2 ' Using an assignment operator to calculate a power of 2. 3 4 Module modAssignment 5 6 Sub Main() 7 Dim exponent AsInteger' power input by user 8 Dim result AsInteger = 2' number to raise to a power 9 10 ' prompt user for exponent 11 Console.Write("Enter an integer exponent: ") 12 result = Console.ReadLine() 13 14 result ^= exponent ' same as result = result ^ exponent 15 Console.WriteLine(“result ^= exponent: {0}", result) 16 17 result = 2' reset base value 18 result = result ^ exponent 19 Console.WriteLine(“result = result ^ exponent: {0}", result) 20 21 EndSub' Main 22 23 EndModule ' modAssignment Assignment.vb輸出 Enter an integer exponent: 8 result ^= exponent: 256 result = result ^ exponent: 256

  28. 4.15 視窗應用程式 • 視窗應用程式 • 至少有一類別 • 繼承表單類別 • 表單繼承基本表單 • 關鍵字Class • 類別定義 class ….end class • 關鍵字 Inherits • 指定類別繼承已存在的其他類別

  29. 4.15視窗應用程式 隱藏收斂 Fig. 4.21 IDE的程式碼編輯視窗.

  30. 4.15視窗應用程式編碼 展開程式碼 Fig. 4.22 視窗應用程式編碼展開

  31. 4.15視窗應用程式 快點設計表單 快點設計程式編碼 標籤屬性設定 Fig. 4.23 ide 產生標籤物件程式碼.

  32. 4.15視窗應用程式 Text 屬性 Fig. 4.24 使用屬性視窗設定屬性初值

  33. 4.15視窗應用程式 Text屬性 Fig. 4.25 表單視窗產生相對應程式碼

  34. 4.15視窗應用程式 Text屬性 Fig. 4.26 在程式碼視窗改變text屬性值

  35. 4.15視窗應用程式 Text屬性 Fig. 4.27 新屬性值於設計環境中改變.

  36. 4.15視窗應用程式 • 編寫程式改變標籤屬性 • 表單載入事件:private sub form1_load(byval sender as system.object,byval e as eventargs) handles mybase_load lblWelcome.Text = “Visual Basic” End sub 3. 選擇 Build > Build Solution then Debug > Start

  37. 4.15視窗應用程式 表單載入事件回應 Fig. 4.28於表單載入事件回應程序撰寫程式

  38. 4.15視窗應用程式 Fig. 4.29 於表單載入事件回應程序含程式碼

  39. 4.15視窗應用程式 • 按關閉紐結束程式

More Related