0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby if文, unless文についてまとめました1

Last updated at Posted at 2023-01-30

条件分岐処理

if文

「もし〇〇だったら□□をする」という処理をします。

if 条件式
  処理1条件式がtrueのときに実行
else
  処理2条件式がfalseのときに実行
end

if文は、条件式が「正しい」か「正しくないか」で実行する処理を分岐させています。

「正しい」 = true

「正しくないか」 = false

条件式が正しい(true)なら、処理1を実行。
条件式が正しくない(false)なら、処理2を実行。

unless文

「もし〇〇だったら□□をする」という処理をします。
unless文はif文と反対で、条件式が「偽(false)」の時に処理が実行されます。
unless文にelsif を指定することはできないです。

公式リファレンスを参考にしました。
https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html#unless

unless 条件式
  処理1条件式がfalseのときに実行
else
  処理2条件式がtrueのときに実行
end

unless文は、if文と同様に条件式が「正しい」か「正しくないか」で実行する処理を分岐させています。

「正しい」 = true

「正しくないか」 = false

条件式が正しくない(false)なら、処理1を実行。
条件式が正しい(true)なら、処理2を実行。

続きです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?