반복문이 없다면
Python
1 2 3 4 5 6 7 8 9 10 | print ( "Hello world 0" ) print ( "Hello world 9" ) print ( "Hello world 18" ) print ( "Hello world 27" ) print ( "Hello world 36" ) print ( "Hello world 45" ) print ( "Hello world 54" ) print ( "Hello world 63" ) print ( "Hello world 72" ) print ( "Hello world 81" ) |
Ruby
1 2 3 4 5 6 7 8 9 10 | puts( "Hello world 0" ) puts( "Hello world 9" ) puts( "Hello world 18" ) puts( "Hello world 27" ) puts( "Hello world 36" ) puts( "Hello world 45" ) puts( "Hello world 54" ) puts( "Hello world 63" ) puts( "Hello world 72" ) puts( "Hello world 81" ) |
실행결과
1 2 3 4 5 6 7 8 9 10 | Hello world 0 Hello world 9 Hello world 18 Hello world 27 Hello world 36 Hello world 45 Hello world 54 Hello world 63 Hello world 72 Hello world 81 |
while
Python
1 2 3 | while False : print ( 'Hello world' ) print ( 'After while' ) |
Ruby
1 2 3 4 | while false do puts( 'Hello world' ) end puts( 'after while' ) |
실행결과
1 | after while |
반복조건
Python
1 2 3 4 | i = 0 while i < 3 : print ( 'Hello world' ) i = i + 1 |
Ruby
1 2 3 4 5 | i = 0 while i < 3 do puts( 'Hello world' ) i = i + 1 end |
실행결과
1 2 3 | Hello world Hello world Hello world |
활용
Python
1 2 3 4 | i = 0 while i < 10 : print ( 'print("Hello world ' + str (i * 9 ) + '")' ) i = i + 1 |
Ruby
1 2 3 4 5 | i = 0 while i < 10 do puts( 'puts("Hello world ' +(i* 9 ).to_s()+ '")' ) i = i + 1 end |
실행결과
1 2 3 4 5 6 7 8 9 10 | print("Hello world 0") print("Hello world 9") print("Hello world 18") print("Hello world 27") print("Hello world 36") print("Hello world 45") print("Hello world 54") print("Hello world 63") print("Hello world 72") print("Hello world 81") |
조건문과 반복문의 합체
Python
1 2 3 4 5 | i = 0 while i < 10 : if i = = 4 : print (i) i = i + 1 |
Ruby
1 2 3 4 5 6 7 | i = 0 while i < 10 do if i == 4 puts(i) end i = i + 1 end |
실행결과
4
Python
1 2 3 4 5 6 7 | i = 0 while i < 10 : if i = = 4 : break print (i) i = i + 1 print ( 'after while' ) |
Ruby
1 2 3 4 5 6 7 8 9 | i = 0 while i < 10 do if i == 4 break end puts(i) i = i + 1 end print( 'after while' ) |
실행결과
1 2 3 4 5 | 0 1 2 3 after while |