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 3 years have passed since last update.

少し複雑な条件分岐

Posted at

数値numが1以上かつ10以下の範囲であればTrueを出力します。
または、outside_modeがTrueの場合は数値numが0以下、11以上であってもTrueを出力します。

この処理を条件分岐を使って記述していきます。

出力例:

in1to10(5, false)
 =>True
in1to10(11, false) 
=>False
in1to10(11  true)
=>True

今回は論理演算子を用います。

# aもbもtrueの場合にtrue
a && b 

# aかbのどちらかがtrueの場合にtrue
a || b 

Rubyの論理演算子では左辺から右辺に条件式を評価し、もし式全体の評価が確定した場合、その時点で評価を行いません。

def in1to10 (num, outside_mode)
  if (num >= 1 && num <= 10) || outside_mode
    puts "True"
  else
    puts "False"
  end
end

このような記述をすることで、出力例のようにし出力が可能となります。

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?