34,333
edits
Changes
no edit summary
If computers do one thing well it is performing tasks very quickly. One thing computers do even better is performing the same task over and over again until specific criteria are met (or even infinitely if that is what is required). One this thing that computers are very bad at, however, is doing anything without being told to do it. Given these facts, it should come as no surprise that just about every programming language, Ruby included, provide mechanisms for instructing a computer system to repeat tasks.
In this chapter of [[Ruby Essentials]] we will look at using ''while'' and ''until'' structures to allow loops to be built into applications. In the next chapter we will look at [[Looping with for and the Ruby Looping Methods|using the for loop and Ruby looping methods]].
== Breaking from While Loops ==
It is sometimes necessary to break out of a while loop before the while expression evaluates to truefalse. This can be achieved using the ''break if'' statement:
<pre>
</pre>
The above loop when will now exit when i equals 2, instead of when i reaches 5.
== unless and until ==