while ループ

Julia中級

Anthony Markham

Quantitative Developer

while ループ - 構文

  • for ループより使用頻度は低いが、有用な場面があります。
  • condition が真の間、expression を繰り返します。
  • ある条件を満たすまで処理を繰り返すときに使います。
while condition
    expression
end
Julia中級

while ループ - 例

  • 条件が真の間、処理を繰り返します
counter = 10
while counter != 0
    print(counter, " ")
    counter = counter - 1
end
10 9 8 7 6 5 4 3 2 1
Julia中級

while ループ - 例 1回目

# 1回目の反復
counter = 10
while counter != 0  # ここで counter = 10 なので true
    print(counter, " ")  # counter(10)を出力
    counter = counter - 1  # counter を 1 減らす
end
10
Julia中級

while ループ - 例 2回目

# 2回目の反復
counter = 10
while counter != 0  # ここで counter = 9 なので true
    print(counter, " ")  # counter(9)を出力
    counter = counter - 1  # counter を 1 減らす
end
10 9
Julia中級

while ループ - 例 3回目

# 3回目の反復
counter = 10
while counter != 0  # ここで counter = 8 なので true
    print(counter, " ")  # counter(8)を出力
    counter = counter - 1  # counter を 1 減らす
end
10 9 8
Julia中級

while ループ - 無限ループ

  • 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中級

while ループ - 終了

  • DataCamp 環境では、無限ループはセッション切断を引き起こします
  • ローカル環境では、Ctrl + C{{1}} で Julia プログラムを終了します
Julia中級

練習しましょう!

Julia中級

Preparing Video For Download...