第 4 章 : 控制結構
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 PresentationTRANSCRIPT
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 視窗應用程式
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
4.4 控制結構
Fig. 4.1 流程圖 Visual Basic’s 順序控制
add grade to total
add 1 to counter
total = total + grade
counter = counter + 1
8
4.4 控制結構
• 選擇結構
• If/Then• 單選結構
– If/Then/Else• 二選結構
– Select Case• 多選結構• Elseif 多選結構
9
4.4 控制結構
• 迴圈結構– While– Do While/Loop– Do/Loop While– Do Until/Loop– Do/Loop Until– For/Next– For Each/Next
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 關鍵字 .
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 關鍵字
12
4.5 If/Then 選擇結構
• 一個進入口及一個出口• 如
If grade >= 40 Then
Console.WriteLine(“ok”)
End If
• If 1>3 then
console.write(“no”)
end if
13
4.5 If/Then 選擇結構
Fig. 4.3 流程圖 Visual Basic’s 單選結構
Grade >= 40true Console.WriteLine(“ok”)
false
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 關鍵字
15
4.6 If/Then/Else 選擇結構
Fig. 4.4 二選結構的流程圖
Grade >= 40true Console.WriteLine(“yes”)false
Console.WriteLine(“no”)
16
4.7 While 的迴圈結構
• 迴圈結構• 依據某些條件值執行迴圈結構的重複敘述• 例如 ( 虛擬碼 )
While more purchasing items
Purchasing next item
Crossing it off my list
大綱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 的值
18
4.7 while 迴圈結構
Fig. 4.6 while 迴圈結構的流程圖
product <= 1000true
product = product * 2
false
194.8 Do While/Loop 迴圈結構
• 與 while 迴圈結構一樣但可分為 a) 前置正向 - do while -loop
b) 後置正向 – do –loop while
大綱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
21
4.8 Do While/Loop while 迴圈結構
Fig. 4.8 do while-loop 迴圈結構的流程圖
product <= 1000true
product = product * 2
false
224.9 Do Until/Loop 迴圈結構
• 前置反向 do until/loop – 測試判定式是否為false
( 判定式在前 )• 後置反向 do –loop until- 測試判定式是否為
false( 判定式在 後 )
大綱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 為真時迴圈執行結束
24
4.9 Do Until/Loop 迴圈結構
Fig. 4.10 do until-loop 迴圈結構的流程圖
product > 1000
true
product = product * 2false
25
4.10 指定運算子與算數指定運算子
• binary operators– +, -, *, ^, &, / or \
變數 = 變數 運算子 表示式變數 運算子 = 表示式
• Example– 加法指定運算子 , +=
value = value + 3
value += 3
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 算數指定運算子
大綱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
算數指定運算子
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 視窗應用程式
Fig. 4.24 使用屬性視窗設定屬性初值
Text 屬性
33
4.15 視窗應用程式
Fig. 4.25 表單視窗產生相對應程式碼
Text 屬性
34
4.15 視窗應用程式
Fig. 4.26 在程式碼視窗改變 text 屬性值
Text 屬性
35
4.15 視窗應用程式
Fig. 4.27 新屬性值於設計環境中改變 .
Text 屬性
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
37
4.15 視窗應用程式
Fig. 4.28 於表單載入事件回應程序撰寫程式
表單載入事件回應
38
4.15 視窗應用程式
Fig. 4.29 於表單載入事件回應程序含程式碼
39
4.15 視窗應用程式
• 按關閉紐結束程式