Ruby is a rich programming language in that it has a large collection of very simple and useful programming tools which make programming in Ruby a very simple and enjoyable task. One such family of these simple and useful tools in Ruby is the Ruby loops. The loops in Ruby are the statements that help in the execution of a certain code repeatedly over a particular number of times. There are a number of loops in Ruby and one such loop is the while loop. The while loop executes a block of code repeatedly a certain number of times till the condition which has been specified remains true. As soon as the condition ceases to be true, the loop breaks and the execution of the code stops.
Here we are going to discuss the syntax and working of the while loop in detail and will try and understand its functioning through some suitable examples. Before we begin our study of the while loop, it would be a good idea to go through some of the basics of the programing in Ruby. This will help you to brush up the basics and in case of beginners, would help in building some kind of a base for further study of the more advanced tools. This course here on the basics of programming in Ruby will be a good place to start.
Talking about the loops and particularly the while loop, these are the most basic and highly useful programming tools in ruby and it is a must to be familiar with these for becoming a good Ruby programmer. Other than the loops, there are several other important tools as well which you must be familiar with for writing programs of all kinds and becoming a versatile programmer. To know more about these tools and about programming in Ruby in general, you can read this great article about learning programming in Ruby.
Now that we are familiar with the basics of programming in Ruby and have brushed up the concepts, we will now discuss about the while loop in detail.
The While Loop
The while loop in Ruby is a programming tool which helps the programmer to execute a particular code repeatedly a number of times till a particular condition remains true. As soon as the condition ceases to be true, the loop terminates. Here we are going to see the syntax of the while loop, will try to understand its functioning, and will have a look at a few examples for proper understanding.
The syntax is the particular form or a kind of a script in which the code needs to be written. Every individual command, statement or tool has its own syntax. The syntax to be used for the while loop is given as:
while condition do
This is the syntax which is used for a while loop in ruby. Here ‘condition’ specifies the condition which needs to be true for the loop to be executed. ‘Code’ in this syntax specifies the code which is executed when the condition for the while loop is true. An example of correctly using this syntax could be,
k = 0
k += 1
Here in this example, the condition is replaced by k<4. This means that as long as k is less then 4, the loop will keep on being executed. Here k has been initialized as 0 and hence the acceptable values of k as per the condition will be 0, 1, 2, 3. As soon as k becomes 4, the loop will terminate and the execution will stop.
The code in this example is,
k += 1
What this code would do is that it will print the current value of k as the output while incrementing the value of k each time the loop is completed.
Here is a great course about programming in Ruby which will teach you about all the different statements and tools and their correct syntax in an exhaustive manner and will enable you to write the basic programs using these programming tools.
Working of the While Loop
The working of the while loop is not at all complex and can be understood quite easily. Let us have a look at the example which has been mentioned above. Here k has been initiated as o and then every time the loop is completed, the value of k is incremented by one. Here the condition has been specified as k<4, and till k remains less than 4, this means for values 0, 1, 2 and 3 the code inside the loop will get executed. As soon as the value of k become 4, the loop terminates. This is how a while loop works. Now any code can be executed for any number of times by specifying the appropriate condition in the loop.
The while loop can also be used as some kind of an alarm where we can specify the task on whose completion we want to get notified as the ‘condition’ and the output which will work as the siren of the alarm as ‘code’. This will help us in knowing exactly when the particular task gets completed as the code will stop being executed as soon as the loop breaks.
Breaking from While Loop
Sometimes the requirement of a program might be that the while loops breaks out before the specified condition becomes false. As per the definition of the while loop, the program must terminate only after the condition ceases to be true, but if an earlier termination is the requirement, then this can be achieved by using the break statement at the appropriate place. An example of the usage of the break statement has been given below.
k = 0
while k < 4
k += 1
break if k == 2
Here the loop in its original for would have terminated when k equals 4, but by the use of the break statement in conjunction with the if statement, we can make the loop to terminate as soon as the value of k becomes equal to 2. This is how the break statement works with a while loop.
This was all about the while loop in Ruby. The while loop provides great flexibility and helps in writing programs more efficiently. When this loop is used along with the other basic programming tools, the programs which can be created are high in functionality and yet very easy to read and write. This is the most important feature about Ruby which has lead to a significant rise in its popularity in recent times.
The programming technique which has been discussed here, the Ruby while loop, is not self sufficient in itself and a good understanding of the other basic concepts as well will enable you to use this tool in the best possible way. Here is a great course about the basic concepts of programming in Ruby which will enable you to write simple programs in Ruby.
If you want to go a step further and want to learn ruby at an advanced level then here is another course which covers the concepts of programming in Ruby right from the basics to all the concepts at an advanced level. This course will help you become a master in programming in Ruby. So go ahead and start learning!