# while による繰り返し(例@) i = 1 while i <= 5 do puts( i.to_s + "回目です。" ) i += 1 end # while による繰り返し(例A) total = 0 i = 1 while i <= 10 do total += i i += 1 end puts( total ) # while による繰り返し(例A') total = 0 i = 1 while total <= 100 do total += i i += 1 end print( i , " " , total ) # while による繰り返し(例B) i = 10 while i != 0 do puts( i ) i -= 1 end i = 10 while i > 0 do puts( i ) i -= 1 end # while による繰り返し(例C) i = 0 while i < 10 do puts( i * 2 ) i += 1 end i = 0 while i <=10 do puts( i * 2 ) i += 1 end # while による繰り返し(例D) i = 1000 while i > 0 do puts( i ) i = i / 2 end i = 2 while i < 100000 do puts( i ) i = i * 2 end # while による繰り返し(例E) i = 1 while i <= 5 do puts( i.to_s + "回目です。" ) end # times と while の違いB i = 2 while i < 100000 do puts( i ) i = i * 2 end # times と while の違いC i = 1 count = 0 while count != 10 do if i % 2 == 0 then print( i , "\n" ) count += 1 end i += 1 end # times と while の違いD count = 0 while count != 10 do x = rand( 100 ) if x % 2 == 0 then print( x , "\n" ) count += 1 end end # times と while の違いE s = 0.0 n = 0 while s<10.0 do r = rand() s += r n += 1 end puts( "#{n} 回目で10を越えた" ) # 擬似乱数の復習 s = 0.0 s2= 0.0 n =10 n.times{ |i| r = rand() s += r s2 += r*r puts( "#{r} は #{i} 番目の乱数です." ) } a = s/n v = (s2 - a*a*n)/(n-1) sd = Math.sqrt( v ) puts( "平均= #{a}, 分散= #{v}, 標準偏差= #{sd}" ) # break文A i = 0 total = 0 while i < 10 do total += i i += 1 break if total > 10 end print( i , " " , total ) # break文B i=0 while true do puts( i ) i=i+1 break if i > 10 end # 無限ループ while true do puts( "こんにちは" ) end # break文C i = 1000 while true do puts( i ) i = i / 2 break if i <= 0 end # break文D 10.times{ |i| puts( i ) break if i > 5 } (10..100).each{ |i| puts( i ) break if i > 20 } # next@ (0..5).each { |i| if ( i==1 || i==4 ) then next end puts( "iは #{i}" ) } # nextA i=0 while i < 10 do i += 1 if i == 5 then next end puts( i ) end # nextB i=0 while i < 10 do i += 1 if i % 2 == 0 then next end puts( i ) end # 繰り返しのまとめA 10.times{ |i| print( i , "\n" ) } (0..9).each{ |i| print( i , "\n" ) } i = 0 loop{ break if i == 10 print( i , "\n" ) i += 1 } i = 0 while i < 10 do print( i , "\n" ) i += 1 end # 繰り返しのまとめB i = 1 loop{ i = i*2 break if i > 100 print( i , "\n" ) } i = 2 while i < 100 do print( i , "\n" ) i = i*2 end i = 1 while true do i = i*2 break if i > 100 print( i , "\n" ) end # downto 10.downto(0){ |x| printf( "x=%2d x^2=%3d\n" , x , x**2 ) } 0.downto(-10){ |x| printf( "x=%3d x^2=%6d\n" , x , x**2 ) } # upto 0.upto(10){ |x| print( "x= " , x , "\n" ) } # step 0.step(10,2){ |x| print( x , "\n" ) } 1.step(10,2){ |x| print( x , "\n" ) } 10.step(0,-2){ |x| print( x , "\n" ) } 9.step(1,-2){ |x| print( x , "\n" ) } # forループ for x in 1..10 do puts( 2**x ) end for x in -10..10 do puts( 2**x ) end total = 0 for x in 1..10 do total += x end print( total )