반복문이 없다면
Python
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
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")
실행결과
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
while False: print('Hello world') print('After while')
Ruby
while false do puts('Hello world') end puts('after while')
실행결과
after while
반복조건
Python
i = 0 while i < 3: print('Hello world') i = i + 1
Ruby
i = 0 while i < 3 do puts('Hello world') i = i + 1 end
실행결과
Hello world Hello world Hello world
활용
Python
i = 0 while i < 10: print('print("Hello world '+str(i*9)+'")') i = i + 1
Ruby
i = 0 while i < 10 do puts('puts("Hello world '+(i*9).to_s()+'")') i = i + 1 end
실행결과
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
i = 0 while i < 10: if i == 4: print(i) i = i + 1
Ruby
i = 0 while i < 10 do if i == 4 puts(i) end i = i + 1 end
실행결과
4
Python
i = 0 while i < 10: if i == 4: break print(i) i = i + 1 print('after while')
Ruby
i = 0 while i < 10 do if i == 4 break end puts(i) i = i + 1 end print('after while')
실행결과
0 1 2 3 after while