When the condition becomes false, the control will be out from the while loop. It is also known as Entry Controlled Loop because the condition to be tested is present at the beginning of the loop body. So basically, while loop is used when the number of iterations is not fixed in a program. It iterates over a specific range of numbers. Here expression can be range or array variable.
Example Range Operators create a range of successive values consisting of a start, end, and range of values in between. The statement for a in It is a Exit-Controlled loop because it tests the condition which presents at the end of the loop body. Ruby program to illustrate 'do.. Skip to content. Change Language. One last note: unlike the loop method, while is not implemented as a method. One consequence of this difference is, that unlike loop , a while loop does not have its own scope -- the entire body of the loop is in the same scope as the code that contains the while loop:.
As you can see, even though y is initialized in the body of the while loop, it's still in scope after the loop finishes running. We didn't mention the until loop in the introduction paragraph. We do, however, need to mention them briefly so that you know about them. The until loop is simply the opposite of the while loop. You can substitute it in order to phrase the problem in a different way.
Let's look briefly at how it works. There are instances when using until will allow you to write code that is more readable and logical.
Ruby has many features for making your code more expressive. The until loop is one of those features. As with while loops, until is not a method. Therefore, until loops do not have their own scope. Let's write some code that asks if the user wants to perform an action again, but we'll keep prompting the user to enter 'Y' until they do. Notice that we're using a simple loop with a break condition at the end of the loop, therefore ensuring that the loop executes at least once.
Try copying and pasting the above code into irb and playing with it. Compare this with a normal "while" loop. In Ruby, for loops are used to loop over a collection of elements. Unlike a while loop where if we're not careful we can cause an infinite loop, for loops have a definite end since it's looping over a finite number of elements. It begins with the for reserved word, followed by a variable, then the in reserved word, and then a collection of elements.
We'll show this using an array and a range. A range is a special type in Ruby that captures a range of elements. For example The odd thing about the for loop is that the loop returns the collection of elements after it executes, whereas the earlier while loop examples return nil.
Let's look at another example using an array instead of a range. In this case, we had to reverse the array to ensure a proper countdown. Otherwise, the loop would have counted up. You can see there are a lot of ways to loop through a collection of elements using Ruby. Let's talk about some more interesting ways you can use conditions to modify the behavior of your loops. Most Rubyists forsake for loops and prefer using iterators instead.
We'll cover iterators later. As with the while and until loops, for is not implemented as a method. Therefore, a for loop does not have its own scope -- the entire body of the loop is in the same scope as the code that contains the for loop. To make loops more effective and precise, we can add conditional flow control within them to alter their behavior. Let's use an if statement in a while loop to demonstrate. This loop uses the odd? If it is, it prints to the screen. Next, x increments by one, and then the loop proceeds to the next iteration.
If you place the next reserved word in a loop, it will jump from that line to the next loop iteration without executing the code beneath it. If you place the break reserved word in a loop, it will exit the loop immediately without executing any more code in the loop.
We use the next reserved word here to avoid printing the number 3 in our loop. Let's try break as well. When you run this program you can see that the entire loop exits when the value of x reaches 7 in the loop. That is why the print out only goes to 5. Loops are basic constructs in any programming language, but most Rubyists, where possible, prefer iterators over loops.
We'll talk about iterators next. Iterators are methods that naturally loop over a given set of data and allow you to operate on each element in the collection. We said earlier that arrays are ordered lists. Let's say that you had an array of names and you wanted to print them to the screen.
How could you do that? You could use the each method for arrays, like this:. We have called the each method using the dot operator. Example: Ruby Ruby program to illustrate each iterator! The collect iterator returns an entire collection, regardless of whether it is an array or hash. The collect method returns the entire collection, regardless of whether it is an array or a hash. Example: Ruby Ruby program to illustrate the collect iterator! The loop is initially started from zero and runs until the one less than the specified number.
This can be used with no iteration variable.. We can add an iteration variable by using the vertical bars around the identifier. Syntax: t. Example: Ruby.
Skip to content. Change Language. Related Articles. Table of Contents. Save Article.
0コメント