LoginSignup
0
0

【Ruby】ifとunless

Last updated at Posted at 2024-07-02

if文

qiita.rb
a = 100
if a == 100 #条件
  puts "aは100です" #条件を満たした時の処理
end

if文は、条件を満たした時に処理を実行。
条件を満たす時とは、条件がtrueの時。

unless文

qiita.rb
a = 200
unless a == 100 #条件
  puts "aは100ではありません" #条件を満たした時の処理
end

unless文は、条件を満たさない時に処理を実行。
条件を満たさない時とは、条件がfalse若しくはnilの時。
unless == if notのイメージ。

なお、下記のようにif文を用いて書き換えも可能。

qiita.rb
a = 200
if a != 100 #条件
  puts "aは100ではありません" #条件を満たした時の処理
end

この場合は、条件(a != 100)がtrueなので、条件を満たした時の処理が実行される。

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