8
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.

第七回

Last updated at Posted at 2020-12-23

条件多岐文

if-elseの記入法

if (conditional expression No.1) then
 process No.1

elsif (conditional expression No.2) then
 process No.2

else
 process No.3

end

caseの記入法

case X
when Alpha then
 process No.1

when Beta then
 process No.2

else
 process No.3

end

Theme 1 閏年を判定するプログラム

閏年の判定方法

1. 閏年は4で割り切れる数の年
2. 1の条件を満たし,且,100で割り切れる年は閏年ではない.
3. 1の条件を満たし,且,400で割り切れる年は閏年である.

2004, 1999, 1900, 2000で試した場合,結果は
true, false, false, true と出力される

プログラム

if-else文を用いたものは以下のようになる.

uruu.rb

p year = ARGV[0].to_i
if year % 4 == 0 then

  if year % 400 ==0 then
    p true

  elsif year % 100 ==0 then
    p false

  else
    p true
  end

else
  p false

end

実行結果

C:\Users\nobua\M1>ruby uruu.rb 2004
2004
true

C:\Users\nobua\M1>ruby uruu.rb 1999
1999
false

C:\Users\nobua\M1>ruby uruu.rb 1900
1900
false

C:\Users\nobua\M1>ruby uruu.rb 2000
2000
true

続いてはcaseを用いたものを示す.

uruu2.rb
def leap(year)
  case
  when year % 400 ==0 ; return true
  when year % 100 ==0 ; return false
  when year % 4 ==0 ;   return true
  else ;                 return false
  end
end

year1=2004
year2=1999
year3=1900
year4=2000

p year1
p year2
p year3
p year4

p leap(year1)
p leap(year2)
p leap(year3)
p leap(year4)

実行結果

C:\Users\nobua\M1>ruby uruu2.rb
2004
1999
1900
2000
true
false
false
true

array,each

参考文献

https://qiita.com/daddygongon/items/a550bde1ed6bff5217d8#case%E3%81%A7


  • source ~/grad_members_20f/members/NobuakiMori/L07_if_else.org
8
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
8
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?