Main Content
Breaking a loop - Ruby and other languages Archive - Originally posted on "The Horse's Mouth" - 2006-12-03 18:30:59 - Graham Ellis
When you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks for the current iteration to be rurun.
With me so far?
Now in Ruby ... you have break , you have next , you have redo and you also have retry ... which reenters the top of the loop. Wow - perhaps I had better provide an example?
for i in 1..5
print "How many do you need on day #{i}? "
nneed = gets.chomp.to_i
# next - don't order nothing; move on to next case
next if nneed == 0
# retry - whole thing to be redone on a 999 code
retry if [112,911,999].include?(nneed)
# redo - more than 10 ordered - must be a mistake
if nneed > 10 then print "Too many!\n"; redo; end
# break - code "-1" entered to exit on a short week
break if nneed == -1
print "We'll get #{nneed} on order for day #{i}\n"
end
By the way - if you've not seen much Ruby before, have a look through that example and taste a few of the constructs. It's really neat, and I expect I'll be writing a lot more about it in the future. ;-) . Source of this example [here] .
New technical articles on Ruby:
How classes are defined and used
Ruby's Control statements
String functions in Ruby
Ruby Regular Expressions
Modules, Mixins and Comparators
Some other articles
R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby Where does Ruby load modules from, and how to load from current directory Separating your code for easier testing, understanding and re-use; example in Ruby Finding sum, minimum, maximum and average in Python (and Ruby) Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Passing code to procedures and yield in Ruby A short form of if ... then ... else Learning to program in Ruby - examples of the programming basics Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) Saying NOT in Perl, PHP, Python, Lua ... Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? This article 1st, 2nd, 3rd revisited in Ruby P206 - More Loops and Conditionals While, for, foreach or something else to loop. Ruby v Perl - a comparison example Perl - making best use of the flexibility, but also using good coding standards How a for loop works Java, Perl and other languages Some more advanced Perl examples from a recent course Multiway branches in Perl - the given and when syntax Alternative loops and conditionals in Ruby and Perl Are you learning Perl? Some more examples for you! A pint of Black Rat, and a lazy barman Setting a safety net or fallback value in Perl switch and case, or given and when in Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Question Mark - Colon operator (Perl and PHP) Saying NOT in Perl, PHP, Python, Lua ... Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator This article The ternary operator in Python What - no switch or case statement? Perl - redo and last without a loop H104 - Control Statements Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) While, for, foreach or something else to loop. Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Predictions for the seagull population Extracting real data from an exported file in PHP or Perl Question Mark - Colon operator (Perl and PHP) Saying NOT in Perl, PHP, Python, Lua ... Decisions - small ones, or big ones? for loop - how it works (Perl, PHP, Java, C, etc) Testing for one of a list of values. Smart English Output - via PHP and Perl ? : operator This article Double and Triple equals operator in PHP The ternary operator in Python Don't repeat code - use loops or functions Assignment, equality and identity in PHP Wimbledon Neck Code and code maintainance efficiency C203 - Conditionals and Loops Learning to program sample program - past its prime, but still useful Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Does a for loop evaluate its end condition once, or on every iteration? Loops - a comparison of goto, while and for Breaking the running sequence - an introduction to conditional statements and loops How a for loop works Java, Perl and other languages New year, new C Course Increment operators for counting - Perl, PHP, C and others Function Prototypes in C New C Examples - pointers, realloc, structs and more Saying NOT in Perl, PHP, Python, Lua ... Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator This article Wimbledon Neck