# while による繰り返し(例@) i = 1 while i <= 5 do puts( i.to_s + "回目です。" ) i += 1 end # 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 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 # break文B i=0 while true do puts( i ) i=i+1 break if i > 10 end # 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 # 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 )