Is There a "Do ... While" Loop in Ruby

Is there a do ... while loop in Ruby?

CAUTION:

The begin <code> end while <condition> is rejected by Ruby's author Matz. Instead he suggests using Kernel#loop, e.g.

loop do 
# some code here
break if <condition>
end

Here's an email exchange in 23 Nov 2005 where Matz states:

|> Don't use it please.  I'm regretting this feature, and I'd like to
|> remove it in the future if it's possible.
|
|I'm surprised. What do you regret about it?

Because it's hard for users to tell

begin <code> end while <cond>

works differently from

<code> while <cond>

RosettaCode wiki has a similar story:

During November 2005, Yukihiro Matsumoto, the creator of Ruby, regretted this loop feature and suggested using Kernel#loop.

What's the purpose of `do` in the while loop?

The non-theoretical answer is quite simply: because the Ruby Syntax Guide said so.

The language syntax guide defines the do keyword as optional for both while and until loops.

From what I understand - and it is mostly a theory - allowing the do to be optional rather than required is mostly for compatibility and allowing for harmonic syntax within the language.

I think of it as an acknowledgment for the "gray" areas, where things are less absolute. The same is done in physics, where light behaves both as a particle and as a wave and we should acknowledge both aspects.

Coming from different languages and school of thought, while is somewhere between a pure keyword (like if) and a method (like loop, which is defined under the Kernel module)

if statements do not require a do to begin a block of code and under the same logic, while loops wouldn't require a do keyword.

This is while as a keyword.

On the other hand, loop requires the do keyword (or the {block}), so why shouldn't while have the same semantics?

This is while as a method (as far as syntax goes).

Ruby is about making the programmer happy. Allowing the do to be optional makes all programmers happy and doesn't require that the Ruby programmer resign themselves to just one school of thought related to the nature of while.

Does Ruby have a built-in do ... while?

...The best I could come up with is the loop construct with a break at the end:

loop do
...
break unless condition
end

While loop inside While working just once in ruby?

After your first loop, the value of i is already 4 and thus no further 'hello' is printed.
Change your code to this:

z = 0
while z < 2
puts "hi coding"
i = 0
while i < 4 do
puts "hello"
i +=1
end
z +=1
end

Ruby while loop with math

You need to have a boolean flag to check at while level each loop:

again = true

while again
puts "Enter a number "
x = Float(gets.chomp())

product = x * 2
puts "Double your number is"
puts product

puts "Go again y/n? "

again = gets.chomp != 'n'
end

What is the difference between the while and until loops in ruby?

ruby allows multiple ways of doing exactly the same thing, so that the code can read naturally depending on which sounds better for you and the code you are writing. Sometimes a conditional works better in the positive eg something_is_happening? vs the negative something_is_done

and while works while something positive is continuing to be positive, whereas until continues the loop until something negative occurs.

eg

while 'yes' == keep_going do
keep_going = get_answer
end

vs

until 'stop' == answer do
answer = get_answer
end

Also i note that you haven't actually tried running your two loops in irb...I know this because the output of the second one is definitely not the same as the first.

2.1.2 :008 > $i = 0
=> 0
2.1.2 :009 > $num = 5
=> 5
2.1.2 :010 >
2.1.2 :011 > until $i < $num do
2.1.2 :012 > puts("Inside the loop i = #$i" )
2.1.2 :013?> $i +=1;
2.1.2 :014 > end
=> nil

This is because when ruby interprets "is $i < $num yet" it evaluates to true and then stops immediately.



Related Topics



Leave a reply



Submit