程式設計基礎:迭代
計算中一個非常重要的部分是迭代的概念,即重複相同的操作。你可能每天都在使用迭代。例如,在留校察看期間寫罰寫;你寫一些句子,檢查是否達到句子限制,如果沒有,你就再寫一些句子,檢查是否達到句子限制,以此類推,直到你達到句子限制,然後你就可以停止了。
'Programmers are lazy and can get computers to write detention lines for them
'they are also lazy as they can do a declaration and assignment at the same time:
dim count as integer = 0
While count <= 100
console.writeline(count & ". I should always do my programming homework.")
count = count + 1
End While
0. 我應該始終完成我的程式設計作業。
1. 我應該始終完成我的程式設計作業。
2. 我應該始終完成我的程式設計作業。
3. 我應該始終完成我的程式設計作業。
...
100. 我應該始終完成我的程式設計作業。
|
理解練習:While 迴圈
在回答這些問題之前,請仔細思考。 答案
|
另一個例子可能是電腦遊戲,其中汽車的速度在加速踏板踩下時一直增加,直到達到最高速度。
dim maxSpeed as integer = 120
dim speedNow as integer = 0
dim pedalDown as boolean = True
While speedNow < maxSpeed And pedalDown
console.writeline(speedNow)
speedNow = speedNow + 1
End While
console.writeline("MAXSPEED!")
|
練習:While 迴圈 答案 dim count as integer = 20
While count <= 60
console.writeline(count)
count = count + 1
End While
編寫一個程式,接收輸入並輸出該數字的乘法表 答案 dim count as integer = 1
dim times as integer
console.write("insert a number: ")
times = console.readline()
While count <= 10
console.writeline(count & " * " & times & " = " & count * times)
count = count + 1
End While
編寫一個程式,將 10 到 20(包含 10 和 20)的所有數字加在一起,最後輸出結果 答案 dim count as integer = 10
dim total as integer = 0
While count <= 20
total = total + count
count = count + 1
End While
console.writeline("the total is: " & total)
|

while 迴圈:例如
While not top speed Do increase speed End
dim speed as integer = 0
While speed < 120
console.writeline(speed)
speed = speed + 1
End While

另一種 while 迴圈是 Do-While 迴圈。它與 While 迴圈略有不同,因為你是在檢查是否需要再次執行任務之前執行任務的。這意味著無論檢查結果如何,你都會執行該任務。
Do increase speed While not top speed End
Visual Basic 使用一些略微不同的語法處理這個問題
console.write("how old are you?")
age = console.readline()
Do
console.writeline(age & " year olds should attend school!")
age = age + 1
Loop Until age > 17
console.writeline(age & " is too old to attend school!")
這對年輕的學生來說很棒
但是,當我們遇到一個 78 歲的老人時,我們就遇到了問題
|
練習:Do While 和 While Do 對於上面那個有問題的例子,將 Do While 重寫為 While Do 迴圈 答案 console.write("how old are you?")
age = console.readline()
While age < 17
console.writeline(age & " year olds should attend school!")
age = age + 1
End While
console.writeline(age & " is too old to attend school!")
|
使用每個迴圈時要小心!

你可能遇到的最複雜的工具是 for 迴圈。這是一個美化後的 While 迴圈,不要被它複雜的外觀嚇倒。它通常也是在 Visual Basic 中進行迭代的最簡單方法之一。
For (speed = 0, not top speed, increase speed) drive
在 vb 中使用它要容易得多
For speed = 0 to 120
drive()
Loop
For 迴圈也允許你向下計數。例如,如果你要建立一個倒計時到目標的計時器。為此,我們使用step - 1程式碼,從而得到以下程式碼
For x = 10 To 1 Step -1
Console.Write(x & ",")
Next
console.writeline("Test over!")
顯示
|
練習:For 迴圈 編寫一個 for 迴圈,顯示 40 次“我會吃我的蔬菜”這句話 答案 for x = 1 to 40
console.writeline("I will eat my greens")
next
編寫一段程式碼,輸入一個較小的數字和一個較大的數字,然後在螢幕上顯示這些數字,從較小的數字開始,依次顯示每個數字,直到達到較大的數字。使用 for 迴圈,它應該顯示以下內容 答案 dim lower, higher as integer
console.write("insert lower number: ")
lower = console.readline()
console.write("insert higher number: ")
higher = console.readline()
For x = lower to higher
console.writeline(x)
Next
編寫一個 for 迴圈,輸出頻率:100,200,300,400,...,20000。提示,你可能想從 1 開始並乘以。記住 答案 For x = 1 to 200
console.beep(x* 100, 100)
Next
讓計算機不斷詢問使用者是否“準備發射?”。如果他們說“是”以外的任何內容,就繼續詢問這個問題。如果他們說“是”,就從 5 開始倒計時,最後顯示“發射!”。 擴充套件:如果你真的想展示你如何使用 case 語句,讓它說:五、四、三、二、一,而不是顯示數字 答案 Dim answer As String
Do
Console.Write("Ready to launch? ")
answer = Console.ReadLine()
Loop While answer <> "Yes"
For x = 5 To 1 Step -1
Console.WriteLine(x)
Next
Console.Write("BLAST OFF!")
|
你已經瞭解了三種主要的迭代型別,其中一些比其他的更適合解決某些問題
- While Do
- Do While
- For
點選這裡瞭解更多資訊
有時,將多個迴圈組合在一起可能是一個好主意。看看這個例子
For x = 1 to 10
console.writeline(x & " : ")
for y = 0 to 10
console.writeline(x & " * " & y & " = " & x * y)
Next
Next
這段程式碼使用巢狀迴圈打印出乘法表。你在學校花了那麼多時間學習的那些乘法表,現在可以用六行程式碼重新編寫了!
1
1 * 0 = 0
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2
2 * 0 = 0
...
|
練習:迴圈的迴圈 答案 For x = 1 to 5
For y = 1 to x
Console.Write("\")
Next
Console.WriteLine()
Next
調整上面的程式碼,讓使用者輸入樹的高度 答案 dim h as integer
console.writeline("please insert a height")
h = console.readline()
For x = 1 to h
For y = 1 to x
Console.Write("\")
Next
Console.WriteLine()
Next
|
