This article mainly introduces the circulation statement summary in Ruby, this article summarizes some commonly used in Ruby the cyclic syntax, the need friend may refer to the next
while (when ...) ) loop |
While condition Statement 1; Statement 2; Statement... End |
Single While loop |
(statement 1; Statement 2; Statement... ) While condition |
Until (until ...) ) loop |
Until condition = While not (condition) |
For...in cycle |
For variable in object Statement 1; Statement 2; Statement... End |
Break |
Jump out of the loop when the layer |
Next |
Ignore the rest of the cycle and start the next loop |
Redo |
Start the cycle again, or from this time |
Retry |
Start over, this cycle body. |
Times |
3.times {print "hi!"} #Hi! Hi! Hi! |
Upto |
1.upto (9) {|i| print i if i<7} #123456 |
Downto |
9.downto (1) {|i| print i if i<7} #654321 |
each |
(1..9). Each {|i| print i if i<7} #123456 |
Step |
0.step (11,3) {|i| Print i} #0369 |