Julia中級
Anthony Markham
Quantitative Developer
for ループより使用頻度は低いが、有用な場面があります。condition が真の間、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
# 1回目の反復
counter = 10
while counter != 0 # ここで counter = 10 なので true
print(counter, " ") # counter(10)を出力
counter = counter - 1 # counter を 1 減らす
end
10
# 2回目の反復
counter = 10
while counter != 0 # ここで counter = 9 なので true
print(counter, " ") # counter(9)を出力
counter = counter - 1 # counter を 1 減らす
end
10 9
# 3回目の反復
counter = 10
while counter != 0 # ここで counter = 8 なので true
print(counter, " ") # counter(8)を出力
counter = counter - 1 # counter を 1 減らす
end
10 9 8
counter 変数の減算を忘れるとどうなりますか?# 2回目の反復
counter = 10
while counter != 0 # ここで counter = 9 なので true
print(counter, " ") # counter(9)を出力
end
10 10 10 10 10 10 10 10 10
Julia中級