Presentation is loading. Please wait.

Presentation is loading. Please wait.

講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所 Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所.

Similar presentations


Presentation on theme: "講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所 Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所."— Presentation transcript:

1 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所
Visual Basic 程式設計 講師:戴志華 國立台灣大學電機工程研究所

2 For Statement 陣列 (Array) Do Statement
第四章 迴圈 For Statement 陣列 (Array) Do Statement

3 For Statement For 變數=初始值 To 結束值 [Step 間隔] 程式碼 Next [變數] 變數=初始值
For i=1 to 10 Print i Next i 變數>結束值? 程式碼 For i=1 To 10 Step 2 Print i Next i 變數=變數+間隔 其它程式碼 當間隔為正數時

4 For Statement(cont’d)
For i=12 To 10 Step -2 Print i Next i 變數=初始值 變數<結束值? For i=12 To 0 Step -2 Print i Next i 程式碼 變數=變數+間隔 For i=1 To 10 Step -2 Print i Next i 其它程式碼 當間隔為負數時

5 Try It! 寫一個程式會連續印Hello, world 5次 Private Sub Form_Click()
Dim i As Integer For i=1 to 5 print “Hello, world” Next i End Sub

6 巢狀迴圈 For迴圈內還有For迴圈 i=1 j=1 j=2 j=3 i=2 For i=1 to 3 i=3 Print “i=”;i
For j=1 to 3 Print “j=”;j Next j Next i

7 Try It * For i=1 to 5 ** For j=1 to i *** Print “*”; **** Next j *****
Next i

8 Try It (cont’d) * For i=1 to 5 ** For j=1 to 5-i *** Print “ ”; ****
***** For i=1 to 5 For j=1 to 5-i Print “ ”; Next j For j=1 to i Print “*”; Print Next i

9 Do…Loop Statement (cont’d)
Do While (比較式) 程式碼 Loop Do 程式碼 Loop While (比較式) 程式碼 比較式成立嗎? 比較式成立嗎? 程式碼 其它程式碼 其它程式碼 兩種方式 差在那?

10 Do…Loop Statement (cont’d)
算1+2+3…+100 用For迴圈 用Do…Loop迴圈 Dim i, counter As Integer i=1 counter=0 Do While (i<=100) counter=counter+i i=i+1 Loop Print counter

11 Do…Loop Statement (cont’d)
中斷迴圈執行 Exit For Exit Do Dim i, counter As Integer i=1 counter=0 Do While (True) counter=counter+i i=i+1 If (i>100) Then Exit Do End If Loop Print counter

12 Do…Loop Statement (cont’d)
Do While (比較式) 程式碼 Loop Do 程式碼 Loop While (比較式) While (比較式) 程式碼 Wend Do 程式碼 Wend While (比較式)

13 Do…Until Statement Do Until (比較式) Do 程式碼 程式碼 Loop Loop Until (比較式) 程式碼
比較式成立嗎? 比較式成立嗎? 程式碼 其它程式碼 其它程式碼

14 陣列-Array Dim intScore1 As Integer Dim intScore2 As Integer ……
totalScore=intScore1+intScore2+…… 如果有100個學生…

15 陣列-Array (cont’d) Dim 變數名 ([下限 To]上限) As 型別 Option Base 0
Dim intScore (39) As Integer Dim intScore (1 to 40) As Integer intScore(0) intScore(1) …… intScore(39) intScore(1) intScore(2) …… intScore(40) Option Base 0 Option Base 1

16 陣列-Array (cont’d) 可以配合For迴圈使用 計算總分 For i=1 to 40
totalScore=totalScore+intScore(i) Next i

17 多維陣列 Dim intScore([下限1 To] 上限1 ,[下限2 To] 上限2) As 型別 假設每個學生都考了三科
Dim intScore(1 To 40, 1 To 3) As Integer intScore(3,2)=90 Dim intScore(1 To 3, 1 To 3) As Integer

18 動態調整陣列大小 宣告陣列時,不指定大小 原先資料會消失 Dim intScore() As Integer
ReDim intScore(40) 原先資料會消失 使用Preserve Dim intScore() As Integer Redim intScore(4) intScore(4)=20 Print intScore(4) 1.[Redim intScore(4)] 2.[Redim Preserve intScore(4)] 注意: 使用Preserve時,ReDim只能改變最後一個維度的大小

19 試試看

20 Project 1 2 3 4 5 6 7 8 9 Hint: ---------------------------- 計算字串長度
1| 2| 3| 4| …… 9| Hint: 計算字串長度 Len(“test”) 將數字轉換成字串 CStr(Number) Deadline:


Download ppt "講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所 Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所."

Similar presentations


Ads by Google