SecondDay

2. Loop

例題：times.rb

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% cat ex2-0.rb
100.times do
puts "I love Haruyo."
end 

例題：for-loop.rb

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% ruby ex2-1.rb
1
2
3
4
5
[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% cat ex2-1.rb
for i in 1..5 do
puts i
end

課題：while.rb

while 条件文 do
繰り返し
end
[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% !r
ruby ex2-2.rb
10
8
6
4
2

課題：二重ループ(printを使って．．)(doubleLoop.rb)

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% !r
ruby ex2-3.rb
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3

課題：九九表(printfを使って)(9x9.rb)

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% ruby ex2-4.rb
1  2  3  4  5  6  7  8  9
2  4  6  8 10 12 14 16 18
3  6  9 12 15 18 21 24 27
4  8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

課題：1から10までの和(sum.rb)

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% ruby ex2-5.rb
55

課題：10!(10*9*8..3*2*1) (factorial.rb)

[bobsNewMacBook:~/Desktop/Ruby/ThreeDays] bob% ruby ex2-6.rb
3628800