How do while loops work with boolean conditions?

Hey all,

I am working through Learn Ruby The Hard Way and came across the
following code:

bear_moved = false

while true
prompt
choice = $stdin.gets.chomp

what does the while true refer to? Does it refer to the bear_moved
variable? Does ruby use the last declared boolean condition or
something?

Hope someone can help!

true is always true. “while true” is an infinite loop, a bit like “loop
do”

Joel P. wrote in post #1154194:

true is always true. “while true” is an infinite loop, a bit like “loop
do”

Ahh thank you! I see, it makes sense now!

So it keeps the logic of the game running until a specific break is
made, for example exit(0)?

Thanks again :slight_smile:

Correct, it will continue to loop until broken by something like
“break”, “exit”, “raise”, etc.
Personally I prefer an explicit “loop” rather than “while true” since
the intention is clearer. Clarity is helpful when debugging code.