Ruby - Statement while loop

Introduction

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

Demo

x = 1 
while x < 100 
 puts x # www  .ja v a 2  s  . c o m
 x = x * 2 
end

Result

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