第 4 章 : 控制結構

39
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 Do While/Loop 大大大大大 4.9 Do Until/Loop 大大大大大 4.10 大大大大大大大大大大大大大 4.15 大大大大大大

Upload: kenneth-garza

Post on 01-Jan-2016

62 views

Category:

Documents


0 download

DESCRIPTION

第 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 介紹. 結構化的程式 控制結構 有助於建立及操作物件. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第  4 章 :  控制結構

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   Do While/Loop 的迴圈結構4.9   Do Until/Loop 的迴圈結構4.10   指定運算子與算數指定運算子4.15   視窗應用程式

Page 2: 第  4 章 :  控制結構

2

4.1 介紹

• 結構化的程式– 控制結構

• 有助於建立及操作物件

Page 3: 第  4 章 :  控制結構

3

4.2 演算法

• 演算法– 解決問題的程序與步驟

• 動作執行的步驟描述 ,

Page 4: 第  4 章 :  控制結構

4

4.3 虛擬碼

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

Page 5: 第  4 章 :  控制結構

5

4.4 控制結構

• 控制轉移– GoTo 敘述

• 易引起程式非結構化使程式不易遵守

– 所有程式可被三種結構完成• 順序結構 (Sequence structure)

• 選擇結構 (Selection structure)

• ( 重複結構 (Repetition structure)

Page 6: 第  4 章 :  控制結構

6

4.4 控制結構

• 流程圖– 圖形化表示演算法– 繪圖使用以下符號

• 方形 - 一般敘述• 菱形 - 判定敘述• Ovals- 輸入輸出• Small circles- 連接

Page 7: 第  4 章 :  控制結構

7

4.4 控制結構

Fig. 4.1 流程圖 Visual Basic’s 順序控制

add grade to total

add 1 to counter

total = total + grade

counter = counter + 1

Page 8: 第  4 章 :  控制結構

8

4.4 控制結構

• 選擇結構

• If/Then• 單選結構

– If/Then/Else• 二選結構

– Select Case• 多選結構• Elseif 多選結構

Page 9: 第  4 章 :  控制結構

9

4.4 控制結構

• 迴圈結構– While– Do While/Loop– Do/Loop While– Do Until/Loop– Do/Loop Until– For/Next– For Each/Next

Page 10: 第  4 章 :  控制結構

10

關鍵字 (keyword)

Visual Basic Keywords

AddHandler AddressOf Alias And

AndAlso Ansi As Assembly Auto Boolean ByRef Byte ByVal Call Case Catch CBool CByte CChar CDate CDec CDbl Char CInt Class CLng CObj Const CShort CSng CStr CType Date Decimal Declare Default Delegate Dim Do Double Each Else ElseIf End Enum Erase Error Event Exit ExternalSource False Finally For Friend Function Get GetType GoTo Handles If Implements Imports In Inherits Integer Interface Is Lib Like Long Loop Me Mod Module MustInherit MustOverride MyBase MyClass Namespace New Next Not Nothing NotInheritable NotOverridable Object On Option Optional Or OrElse Overloads Overridable Overrides ParamArray Preserve

Fig. 4.2 vb.net 關鍵字 .

Page 11: 第  4 章 :  控制結構

11

關鍵字 (keyword)

Private Property Protected Public RaiseEvent ReadOnly ReDim Region Rem RemoveHandler Resume Return Select Set Shadows Shared Short Single Static Step Stop String Structure Sub SyncLock Then Throw To True Try TypeOf Unicode Until When While With WithEvents WriteOnly Xor #Const #If...Then...#Else - -= &

&= * *= / /= \ \= ^ ^= + += = The following are retained as keywords, although they are no longer supported in Visual Basic.NET Let Variant Wend

Fig. 4.2 Visual Basic keywords.

Fig. 4.2 vb.net 關鍵字

Page 12: 第  4 章 :  控制結構

12

4.5 If/Then 選擇結構

• 一個進入口及一個出口• 如

If grade >= 40 Then

Console.WriteLine(“ok”)

End If

• If 1>3 then

console.write(“no”)

end if

Page 13: 第  4 章 :  控制結構

13

4.5 If/Then 選擇結構

Fig. 4.3 流程圖 Visual Basic’s 單選結構

Grade >= 40true Console.WriteLine(“ok”)

false

Page 14: 第  4 章 :  控制結構

14

4.6 If/Then/Else 選擇結構

• ExampleIf Grade >= 40 Then

Console.WriteLine(“ok”)

Else

Console.WriteLine(“no”)

End If

• 巣狀 If/Then/Else 結構– If ---Else 結構包含 if ---lese 結構形成多選 – ElseIf 關鍵字

Page 15: 第  4 章 :  控制結構

15

4.6 If/Then/Else 選擇結構

Fig. 4.4 二選結構的流程圖

Grade >= 40true Console.WriteLine(“yes”)false

Console.WriteLine(“no”)

Page 16: 第  4 章 :  控制結構

16

4.7 While 的迴圈結構

• 迴圈結構• 依據某些條件值執行迴圈結構的重複敘述• 例如 ( 虛擬碼 )

While more purchasing items

Purchasing next item

Crossing it off my list

Page 17: 第  4 章 :  控制結構

大綱17

While.vb

輸出

1 ' Fig. 4.5: While.vb2 ' Demonstration of While structure.3 4 Module modWhile5 6 Sub Main()7 Dim product As Integer = 28 9 ' structure multiplies and displays product10 ' while product is less than 100011 While product <= 100012 Console.Write("{0} ", product)13 product = product * 214 End While15 16 Console.WriteLine() ' write a blank line17 18 ' print result19 Console.WriteLine("Smallest power of 2 " & _20 "greater than 1000 is {0}", product)21 Console.ReadLine() ' prevents window from closing22 End Sub ' Main23 24 End Module ' modWhile

2 4 8 16 32 64 128 256 512Smallest power of 2 greater than 1000 is 1024

每一次測驗 product 的值

Page 18: 第  4 章 :  控制結構

18

4.7 while 迴圈結構

Fig. 4.6 while 迴圈結構的流程圖

product <= 1000true

product = product * 2

false

Page 19: 第  4 章 :  控制結構

194.8 Do While/Loop 迴圈結構

• 與 while 迴圈結構一樣但可分為 a) 前置正向 - do while -loop

b) 後置正向 – do –loop while

Page 20: 第  4 章 :  控制結構

大綱20

DoWhile.vb

輸出

1 ' Fig. 4.7: DoWhile.vb2 ' Demonstration of the Do While/Loop structure.3 4 Module modDoWhile5 6 Sub Main()7 Dim product As Integer = 28 9 ' structure multiplies and displays10 ' product while product is less than 100011 Do While product <= 100012 Console.Write("{0} ", product)13 product = product * 214 Loop15 16 Console.WriteLine() ' write a blank line17 18 ' print result19 Console.WriteLine("Smallest power of 2 " & _20 "greater than 1000 is {0}", product)21 Console.ReadLine() ' prevent window from closing22 End Sub23 24 End Module ' modDoWhile

2 4 8 16 32 64 128 256 512Smallest power of 2 greater than 1000 is 1024

Page 21: 第  4 章 :  控制結構

21

4.8 Do While/Loop while 迴圈結構

Fig. 4.8 do while-loop 迴圈結構的流程圖

product <= 1000true

product = product * 2

false

Page 22: 第  4 章 :  控制結構

224.9 Do Until/Loop 迴圈結構

• 前置反向 do until/loop – 測試判定式是否為false

( 判定式在前 )• 後置反向 do –loop until- 測試判定式是否為

false( 判定式在 後 )

Page 23: 第  4 章 :  控制結構

大綱23

DoUntil.vb

輸出

1 ' Fig. 4.9: DoUntil.vb2 ' Demonstration of the Do Until/Loop Structure.3 4 Module modDoUntil5 6 Sub Main()7 Dim product As Integer = 28 9 ' find first power of 2 greater than 100010 Do Until product > 100011 Console.Write("{0} ", product)12 product = product * 213 Loop14 15 Console.WriteLine() ' write a blank line16 17 ' print result18 Console.WriteLine("Smallest power of 2 " & _19 "greater than 1000 is {0}", product)20 End Sub ' Main2122 End Module ' modDoUntil

2 4 8 16 32 64 128 256 512Smallest power of 2 greater than 1000 is 1024

當 product > 1000 為真時迴圈執行結束

Page 24: 第  4 章 :  控制結構

24

4.9 Do Until/Loop 迴圈結構

Fig. 4.10 do until-loop 迴圈結構的流程圖

product > 1000

true

product = product * 2false

Page 25: 第  4 章 :  控制結構

25

4.10 指定運算子與算數指定運算子

• binary operators– +, -, *, ^, &, / or \

變數 = 變數 運算子 表示式變數 運算子 = 表示式

• Example– 加法指定運算子 , +=

value = value + 3

value += 3

Page 26: 第  4 章 :  控制結構

26

4.10 算數指定運算子

加法指定運算子 例子 說明 指定

Assume: c = 4, d =

"He"

+= c += 7 c = c + 7 11 to c -= c -= 3 c = c - 3 1 to c *= c *= 4 c = c * 4 16 to c /= c /= 2 c = c / 2 2 to c \= c \= 3 c = c \ 3 1 to c ^= c ^= 2 c = c ^ 2 16 to c &= d &= "llo" d = d & "llo" "Hello" to d Fig. 4.11 算數指定運算子

Fig. 4.11 算數指定運算子

Page 27: 第  4 章 :  控制結構

大綱27

Assignment.vb

輸出

1 ' Fig. 4.12: Assignment.vb2 ' Using an assignment operator to calculate a power of 2.3 4 Module modAssignment5 6 Sub Main()7 Dim exponent As Integer ' power input by user8 Dim result As Integer = 2 ' number to raise to a power9 10 ' prompt user for exponent11 Console.Write("Enter an integer exponent: ")12 result = Console.ReadLine()13 14 result ^= exponent ' same as result = result ^ exponent15 Console.WriteLine(“result ^= exponent: {0}", result)16 17 result = 2 ' reset base value18 result = result ^ exponent19 Console.WriteLine(“result = result ^ exponent: {0}", result)20 21 End Sub ' Main2223 End Module ' modAssignment

Enter an integer exponent: 8result ^= exponent: 256result = result ^ exponent: 256

算數指定運算子

Page 28: 第  4 章 :  控制結構

28

4.15 視窗應用程式

• 視窗應用程式– 至少有一類別

• 繼承表單類別• 表單繼承基本表單

– 關鍵字 Class• 類別定義 class ….end class

– 關鍵字 Inherits• 指定類別繼承已存在的其他類別

Page 29: 第  4 章 :  控制結構

29

4.15 視窗應用程式

Fig. 4.21 IDE 的程式碼編輯視窗 .

隱藏收斂

Page 30: 第  4 章 :  控制結構

30

4.15 視窗應用程式編碼

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

展開程式碼

Page 31: 第  4 章 :  控制結構

31

4.15 視窗應用程式

Fig. 4.23 ide 產生標籤物件程式碼 .

快點設計表單 快點設計程式編碼

標籤屬性設定

Page 32: 第  4 章 :  控制結構

32

4.15 視窗應用程式

Fig. 4.24 使用屬性視窗設定屬性初值

Text 屬性

Page 33: 第  4 章 :  控制結構

33

4.15 視窗應用程式

Fig. 4.25 表單視窗產生相對應程式碼

Text 屬性

Page 34: 第  4 章 :  控制結構

34

4.15 視窗應用程式

Fig. 4.26 在程式碼視窗改變 text 屬性值

Text 屬性

Page 35: 第  4 章 :  控制結構

35

4.15 視窗應用程式

Fig. 4.27 新屬性值於設計環境中改變 .

Text 屬性

Page 36: 第  4 章 :  控制結構

36

4.15 視窗應用程式

1. 編寫程式改變標籤屬性2. 表單載入事件 :private sub form1_load(byval

sender as system.object,byval e as eventargs) handles mybase_loadlblWelcome.Text = “Visual Basic”

End sub

3. 選擇 Build > Build Solution then Debug > Start

Page 37: 第  4 章 :  控制結構

37

4.15 視窗應用程式

Fig. 4.28 於表單載入事件回應程序撰寫程式

表單載入事件回應

Page 38: 第  4 章 :  控制結構

38

4.15 視窗應用程式

Fig. 4.29 於表單載入事件回應程序含程式碼

Page 39: 第  4 章 :  控制結構

39

4.15 視窗應用程式

• 按關閉紐結束程式