Julia 中級
Anthony Markham
Quantitative Developer
for 迴圈少見,但在部分情境很實用。condition 為 true 時,重複執行 expression。while condition
expression
end
counter = 10
while counter != 0
print(counter, " ")
counter = counter - 1
end
10 9 8 7 6 5 4 3 2 1
# 第一次迭代
counter = 10
while counter != 0 # 這裡 counter = 10,所以為 true
print(counter, " ") # 列印 counter,等於 10
counter = counter - 1 # 將 counter 減 1
end
10
# 第二次迭代
counter = 10
while counter != 0 # 現在 counter = 9,所以為 true
print(counter, " ") # 列印 counter,等於 9
counter = counter - 1 # 將 counter 減 1
end
10 9
# 第三次迭代
counter = 10
while counter != 0 # 現在 counter = 8,所以為 true
print(counter, " ") # 列印 counter,等於 8
counter = counter - 1 # 將 counter 減 1
end
10 9 8
counter 變數,會發生什麼事?# 第二次迭代
counter = 10
while counter != 0 # 現在 counter = 9,所以為 true
print(counter, " ") # 列印 counter,等於 9
end
10 10 10 10 10 10 10 10 10
Julia 中級