1 / 28

第 4 章 VBScript 的流程控制

第 4 章 VBScript 的流程控制. 4-1 流程控制的基礎 4-2 VBScript 的條件控制 4-3 VBScript 的迴路控制. 4-1 流程控制的基礎. 流程控制指令可以分為兩類,如下所示: 條件控制:條件控制就是一個選擇題,可能為單一選擇或多選一,依照第四章條件運算子的結果,決定執行那一區塊的程式碼。 迴路控制:迴路控制是重複執行一區塊的程式碼,其中擁有一結束條件,以便結束迴路的執行。. 4-2 VBScript 的條件控制. 4-2-1 If … Then 是否選條件敘述

waldo
Télécharger la présentation

第 4 章 VBScript 的流程控制

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章 VBScript的流程控制 • 4-1 流程控制的基礎 • 4-2 VBScript的條件控制 • 4-3 VBScript的迴路控制

  2. 4-1 流程控制的基礎 • 流程控制指令可以分為兩類,如下所示: • 條件控制:條件控制就是一個選擇題,可能為單一選擇或多選一,依照第四章條件運算子的結果,決定執行那一區塊的程式碼。 • 迴路控制:迴路控制是重複執行一區塊的程式碼,其中擁有一結束條件,以便結束迴路的執行。

  3. 4-2 VBScript的條件控制 • 4-2-1 If … Then是否選條件敘述 • 4-2-2 If … Then … Else兩選一條件敘述 • 4-2-3 If Then ElseIf多選一條件敘述 • 4-2-4 Select Case多選一條件敘述

  4. 4-2-1 If … Then是否選條件敘述-1 • If Then條件敘述是一種是否執行的條件,決定是否執行區塊內的程式碼,如果If條件為True,就執行Then...End If間的程式碼,如下所示: If strSex = "男" Then Response.Write(strName & "男士您好! ") Response.Write("歡迎使用ASP 3.0<br>") End If

  5. 4-2-1 If … Then是否選條件敘述-2

  6. 4-2-2 If … Then … Else兩選一條件敘述-1 • 條件如果擁有排它情況的兩個執行區塊,只能二選一,可以加上Else指令,如果If條件為True,就執行Then...Else間的程式碼,False就執行Else...End If間的程式碼,如下所示: If strSex = "男" Then Response.Write(strName & "男士您好! ") Else Response.Write(strName & "女士您好!<br>") End If

  7. 4-2-2 If … Then … Else兩選一條件敘述-2

  8. 4-2-3 If Then ElseIf多選一條件敘述-1 • If Then ElseIf條件敘述屬於If Then條件敘述的延伸,使用ElseIf指令建立多選一條件: dtWeekday = Weekday(Date) If dtWeekday = 1 Then strWdstring="星期日" ElseIf dtWeekday = 2 Then strWdstring="星期一" ElseIf dtWeekday = 3 Then ……….. Else strWdstring="無法分辨是星期幾" End If

  9. 4-2-3 If Then ElseIf多選一條件敘述-2

  10. 4-2-4 Select Case多選一條件敘述-1 • Select Case直接依照符合的條件執行不同區塊的程式碼,如下所示: dtWeekday = Weekday(Date) Select Case dtWeekday Case 1: strWdstring="星期日" Case 2: strWdstring="星期一" Case 3: strWdstring="星期二" Case 4: strWdstring="星期三" Case 5: strWdstring="星期四" Case 6: strWdstring="星期五" Case 7: strWdstring="星期六" Case Else strWdstring="無法分辨是星期幾" End Select

  11. 4-2-4 Select Case多選一條件敘述-2

  12. 4-3 VBScript的迴路控制 • 4-3-1 For … Step … Next迴路 • 4-3-2 巢狀的For Next迴路 • 4-3-3 For Each … In … Next迴路 • 4-3-4 Exit For中斷For迴路 • 4-3-5 Do …Exit Do … Loop迴路 • 4-3-6 Do While … Loop迴路 • 4-3-7 Do Until … Loop迴路 • 4-3-8 Do … Loop While迴路 • 4-3-9 Do … Loop Until迴路 • 4-3-10 Do/While … Loop/Until的巢狀迴路 • 4-3-11 While … Wend迴路

  13. 4-3-1 For … Step … Next迴路-1 • For Step Next迴路指令執行固定次數的迴路,其結束條件是Step量每次增加或減少一個值,如果到達結束條件的值,就結束迴路,如下所示: Dim intI, intTotal For intI = 1 To 10 Step 1 intTotal = intTotal + intI Next

  14. 4-3-1 For … Step … Next迴路-2

  15. 4-3-2 巢狀的For Next迴路 • 巢狀迴路指的是在For Next迴路中擁有其它For Next迴路,有如巢狀般排列的迴路,如下所示: For intI = 1 To 9 For intJ = 1 To 9 …… Next Next

  16. 4-3-3 For Each … In … Next迴路 • For Each迴路使用在物件的資料集合或陣列,顯示資料集合或陣列的所有元素,特別適合那些不知道到底有多少元素的資料集合或陣列,如下所示: For Each strName in name Response.Write("<td>" & strName & "</td>") Next • 上述程式碼變數name為一個陣列,這個迴路可以取得所有陣列的元素。

  17. 4-3-4 Exit For中斷For迴路 • 在迴路尚來到達結束條件時,我們可以使用Exit For指令強迫跳出For Next迴路,結束迴路的執行,如下所示: For intI = 1 To 100 Step 1 … Exit For … Next

  18. 4-3-5 Do …Exit Do … Loop迴路 • Do Loop迴路屬於一個無窮迴路,我們可以使用Exit Do指令結束迴路,如下所示: Do …. Exit Do IntI = intI + 1 …. Loop

  19. 4-3-6 Do While … Loop迴路-1 • Do Loop迴路使用While條件在迴路開頭檢查,開頭檢查的目的是是否允許進入迴路,所以While指令的測試條件如果成立時才能進入迴路,如下所示: intI = 1 intTotal = 0 Do While intI <=10 intTotal = intTotal + intI intI = intI + 1 Loop

  20. 4-3-6 Do While … Loop迴路-2

  21. 4-3-7 Do Until … Loop迴路-1 • Do Loop迴路使用Until條件在迴路開始檢查,Until是直到條件成立,如果條件不成立就進入迴路,如下所示: intI = 1 intTotal = 0 Do Until intI >10 intTotal = intTotal + intI intI = intI + 1 Loop

  22. 4-3-7 Do Until … Loop迴路-2

  23. 4-3-8 Do … Loop While迴路-1 • Do Loop迴路使用While條件在迴路結尾檢查,這個Do Loop迴路至少執行一次,如下所示: intI = 1 intTotal = 0 Do intTotal = intTotal + intI intI = intI + 1 Loop While intI <=10

  24. 4-3-8 Do … Loop While迴路-2

  25. 4-3-9 Do … Loop Until迴路-1 • Do Loop迴路使用Until條件在迴路結尾檢查,這個迴路至少執行一次,如下所示: intI = 1 intTotal = 0 Do intTotal = intTotal + intI intI = intI + 1 Loop Until intI > 10

  26. 4-3-9 Do … Loop Until迴路-2

  27. 4-3-10 Do/While … Loop/Until的巢狀迴路 • Do Loop迴路如同For Next迴路可以結合各種組合建立巢狀迴路,如下所示: Do While intI <= 9 intJ = 1 Do Response.Write(intI & "*" & intJ & "=" & intI*intJ) intJ = intJ + 1 Loop Until intJ > 9 intI = intI + 1 Loop

  28. 4-3-11 While … Wend迴路 • While Wend迴路是在迴路開始時測試條件,以決定是否執行迴路的程式碼,同樣的While Wend迴路也可以建立巢狀迴路,其架構如下所示: While intI <= 9 intJ = 1 While intJ <= 9 Response.Write(intI & "*" & intJ & "=" & intI*intJ) intJ = intJ + 1 Wend intI = intI + 1 Wend

More Related