4 Replies - 3056 Views - Last Post: 03 August 2015 - 03:44 AM Rate Topic: -----

#1 DecompileCodex   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 8
  • Joined: 01-August 15

Difference between 'while' and 'until' loops

Posted 02 August 2015 - 04:21 PM

Is there a distinctive difference between the 'while' and 'until' loops in ruby?

If there is where would we use one over the other?
Is This A Good Question/Topic? 1
  • +

Replies To: Difference between 'while' and 'until' loops

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5466
  • View blog
  • Posts: 14,421
  • Joined: 18-April 07

Re: Difference between 'while' and 'until' loops

Posted 02 August 2015 - 04:35 PM

They are mirrors of one another...

While - do while something is true - "Do while i is less than 10" or "Do while blnFlag is true"

Unless - do until something is true - "Unless i is 10, do this..." or "Unless i is even, don't print this line"


You typically should design loop conditions to be in the affirmative since that is often the easier way to think of something. "While this is true, do something". Unless is a bit in the negative mindset "Unless this is true, do something". You can write either in either way makes sense at the time. You often find while loops much more prevalent in code of all languages than any kind of unless statement.

:)

Examples for you...

while i < 10 equals unless i >= 10

while blnflag == true is like saying unless blnflag == false

This post has been edited by Martyr2: 02 August 2015 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Difference between 'while' and 'until' loops

Posted 02 August 2015 - 04:39 PM

Off-topic nonsense:

I wonder why we don't have a "while.. unless" loop?

while x < 10 unless x == 5

This post has been edited by andrewsw: 02 August 2015 - 04:39 PM

Was This Post Helpful? 1
  • +
  • -

#4 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Re: Difference between 'while' and 'until' loops

Posted 03 August 2015 - 12:29 AM

Because it would be redundant. 9/10 times if you're using a while/until loop or for loop in Ruby, you're doing it wrong.
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Difference between 'while' and 'until' loops

Posted 03 August 2015 - 03:44 AM

Alternative approaches for a simple loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1