Does Ruby Have a "do...while" Loop?

Ruby does not have a built-in "do...while" loop. However, you can use the loop and break syntax to emulate the behavior of a "do...while" loop:

i = 0

loop do
    i += 1

    if i == 4

print i #=> 4

Similar to a "do...while" loop, this loop would execute at least one time, until a break is encountered.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.