初心に戻って基本を学ぶシリーズを始めます。
初回のゲストは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まで出力されます。
ではまた!