Ruby/Statement/until
Материал из Wiki.crossplatform.ru
(Различия между версиями)
Версия 17:10, 26 мая 2010
As unless is a negated form of if, until is really a negated form of while.
# Compare the following statements. The first is a while loop: weight = 150 while weight < 200 do puts "Weight: " + weight.to_s weight += 5 end # Here is the same logic expressed with until: weight = 150 until weight == 200 do puts "Weight: " + weight.to_s weight += 5 end
Here"s the formal specification for the until loop:
until condition [ do | : ] code end
like while, you have another form you can use with untila?that is, with begin/end:
weight = 150 begin puts "Weight: " + weight.to_s weight += 5 end until weight == 200
unless and until
An unless statement is really like a negated if statement if lang == "de" dog = "Hund" else dog = "dog" end # Now I"ll translate it into unless: unless lang == "de" dog = "dog" else dog = "Hund" end
until keeps looping while its condition remains false, or not true.
until($_ == "q") puts "Running..." print "Enter q to quit: " gets chomp end
until loops until a certain condition is met
x = 1 until x > 99 puts x x = x * 2 end