# While vs loop

**URL:** <https://www.ruby-forum.com/t/while-vs-loop/160310>\
**Category:** Ruby\
**Created:** [February 14, 2009, 7:04am UTC](https://www.ruby-forum.com/t/while-vs-loop/160310 "2009-02-14T07:04:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 7:04am UTC](https://www.ruby-forum.com/t/while-vs-loop/160310/1 "2009-02-14T07:04:36Z")

</div>

what is the Difference between loop and while in ruby ?

loop do  
end

while 1  
end

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 7:12am UTC](https://www.ruby-forum.com/t/while-vs-loop/160310/2 "2009-02-14T07:12:58Z")

</div>

Loop will continue indefinitely until it meets with some break statement  
somewhere in its loop body.

While can also be terminated with a break statement, but more  
traditionally,  
it ends when the condition specified in the while statement is  
satisfied.  
If you have “while 1” as you’ve written here though, the two should be  
functionally equivalent.

On Sat, Feb 14, 2009 at 7:03 AM, Vetrivel V.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 7:19am UTC](https://www.ruby-forum.com/t/while-vs-loop/160310/3 "2009-02-14T07:19:24Z")

</div>

Hi,

At Sat, 14 Feb 2009 15:03:03 +0900,  
Vetrivel V. wrote in [ruby-talk:328175]:

> what is the Difference between loop and while in ruby ?
> 
> loop do  
> end

loop is a kernel method which takes a block. A block  
introduces new local variable scope.

loop do  
a = 1  
break  
end  
p a #=\> causes NameError

> while 1  
> end

while doesn’t.

while 1  
a = 1  
break  
end  
p a #=\> 1

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 20, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/while-vs-loop/160310/4 "2009-02-20T23:18:39Z")

</div>

Hi,

Am Samstag, 14. Feb 2009, 15:16:16 +0900 schrieb Nobuyoshi N.:

> At Sat, 14 Feb 2009 15:03:03 +0900,  
> Vetrivel V. wrote in [ruby-talk:328175]:
> 
> > what is the Difference between loop and while in ruby ?
> 
> loop is a kernel method which takes a block. A block  
> introduces new local variable scope.
> 
> while doesn’t.

In other words:

$ irb  
irb(main):001:0\> Kernel.private\_instance\_methods.grep /loop/  
=\> [“loop”]  
irb(main):002:0\> Kernel.private\_instance\_methods.grep /while/  
=\>

Bertram

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/while-vs-loop/160310/5 "2009-02-14T20:35:10Z")

</div>

On Sat, Feb 14, 2009 at 7:16 AM, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> introduces new local variable scope.  
> while doesn’t.
> 
> while 1  
> a = 1  
> break  
> end  
> p a #=\> 1
> 
> –  
> Nobu Nakada

furthermore loop do has an implicit rescue clause for a StopIteration  
exception  
(I believe 1.8.7 and 1.9.1 only IIRC)

therefore

loop do  
some\_enumerator.next  
end  
becomes a convenient idiom.

HTH  
Robert
