Ruby - Statement while loop


while allow you to loop code based on the result of a comparison made on each loop:


x = 1 
while x < 100 
 puts x # from  w  w w .  j a  v a 2s  .co  m
 x = x * 2 


Here, you have a while block that denotes a section of code that is to be repeated over and over while the expression x < 100 is satisfied.

Therefore, x is doubled loop after loop and printed to the screen.

Once x is 100 or over, the loop ends.

Related Topics