Home » Ruby while and do-while loop

Ruby while and do-while loop

by Online Tutorials Library

Ruby while Loop

The Ruby while loop is used to iterate a program several times. If the number of iterations is not fixed for a program, while loop is used.

Ruby while loop executes a condition while a condition is true. Once the condition becomes false, while loop stops its execution.

Syntax:

Ruby while loop 1

Example:

Output:

Ruby while loop 2


Ruby do while Loop

The Ruby do while loop iterates a part of program several times. It is quite similar to a while loop with the only difference that loop will execute at least once. It is due to the fact that in do while loop, condition is written at the end of the code.

Syntax:

Example:

Output:

Ruby while loop 3


Next TopicRuby until loop

You may also like