While Loop Ruby

Madiwa Simon
2 min readJun 20, 2021

Simple way to understand while loop for ruby.

Executes code while conditional is true. A while loop’s conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;.

A specific condition can be put in the while loop no matter what it is as long as the condition is met and true, it will execute the code given inside the while loop.

This while loop will check if the index is greater than or equals 1, since the index is 6 the code provided will execute puts index and will give the current value of index 6 and index -=1 will minus 1 to the current value of index which is 6. The index will now be updated to 5 since it has been deducted by 1. While loop will go on until the Condition is false. It will keep putting the index value in the screen and deducting it 1 until the index has reached the value below its limit which is 0.

Another example.

It has the same concept but different condition. The result of this will add 1 to index until the condition comes out false. which will give the outcome below.

It puts the value of index and add 1 each time it goes through the while loop. The while loop ends it at 6 and does not show it anymore. The value of the index will remain at 6.

--

--