1
1

More than 3 years have passed since last update.

whileで繰り返し ❏Ruby❏

Last updated at Posted at 2019-11-19

初心に戻って基本を学ぶシリーズを始めます。
初回のゲストはwhileさんです。

whileさんは条件式がtrueの場合のみ中の処理を実行してくれます。

使い方

while 条件式
  処理
end

1から100まで出力したいとき

num = 1
while num <= 100
  puts num
  num += 1
end

途中で処理を止めたいとき

breakを使います。

num = 1
while num <= 100
  puts num
  num += 1

  if num == 50
    break
  end
end

numが50になったら処理を抜けます。
結果は1から49まで出力されます。



ではまた!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1