Table of contents
1.
Introduction
2.
Types of Loops in Ruby
3.
The For/in Loop in Ruby
4.
Syntax
5.
Example
6.
Frequently Asked Questions
6.1.
What is Ruby?
6.2.
Who invented the Ruby language?
6.3.
What is the difference between the while loop and for loop in Ruby?
6.4.
Why for loop in Ruby is also known as an entry-controlled loop?
6.5.
Why a do-while loop is also known as an exit control loop?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

For Loop in Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

As we all know, the loop enables us to perform n number of steps together in one line. A loop also allows us to do the repetitive operation in some portion of the program either a particular condition is satisfied or the number of iterations is specified.

In ruby, there are generally four types of loops (for loop, while loop, do-while loop, until loop).

for loop in ruby

Types of Loops in Ruby

Ruby provides different types of loops, such as.

  • for loop - The for/in loop iterates through the elements of an enumerable object (such as an array). Each iteration assigns an element to a specified loop variable and executes the loop's body. for loop is preferred when we know the number of times loop statements are executed.
  • while loop - In a while loop, The condition to be tested is given at the beginning of the loop, and the loop executes the statements until the condition is satisfied. When the condition becomes false, the control goes out. while loop is used when the execution of the statement is not fixed in a program. 
  • do-while loop - A do-while loop is similar to a while loop. The only difference is that it checks the condition at last. Therefore even if the condition is false, the loop executes the statements at least once.
  • until loop - Until the loop executes the statements until the condition is true, it is the opposite of the while loop as the while loop executes the statements until the condition is false.


In this article, we will discuss the for loop in ruby.

The For/in Loop in Ruby

The for/in loop iterates through the elements of an enumerable object (such as an array). Each iteration assigns an element to a specified loop variable and executes the loop's body.

for loop is preferred when we know the number of times loop statements are executed.

Let's see the syntax and some examples of for loop in ruby.

Syntax

Below is the syntax for the for loop in Ruby.

for variable_name expression(range, array variable) [d]
#body of the loop
end
You can also try this code with Online Ruby Compiler
Run Code


for- It is a keyword.

variable_name- It refers to the variable's name that refers to the current iteration of the loop.

expression- It refers to the execution of the code once for each element.

do- It is optional. It indicates the beginning of the block of code.

end- This is used to terminate the for loop or indicate the end of the loop.

Example

Let's see some examples of for loop in Ruby.

Example 1

Input:

a="Hello"
For i in a do
puts i
end
You can also try this code with Online Ruby Compiler
Run Code

Output:

Hello
You can also try this code with Online Ruby Compiler
Run Code


Example 2

Input:

arr=["For", "Loop", “in”, “Ruby”]
for i in a do
puts i
end
You can also try this code with Online Ruby Compiler
Run Code

Here, we declared an array arr.

Output:

For
Loop
in
Ruby
You can also try this code with Online Ruby Compiler
Run Code


Example 3

Input:

for i in 1..7 do
puts i
end
You can also try this code with Online Ruby Compiler
Run Code

Here, we defined the range, so we have used two-dot to include both 1 and 10. (two-dot operators are inclusive operators)

Output:

1
2
3
4
5
6
7
You can also try this code with Online Ruby Compiler
Run Code


We are done with the blog "for loop in ruby," so let's look at some Frequently Asked Questions related to them.

Frequently Asked Questions

What is Ruby?

Ruby is a programming language mainly used in web programming. Ruby is known among programmers for a terse, uncluttered syntax that doesn't require a lot of extra punctuation. Ruby is a modem language that makes it easy to use high-level abstractions such as metaprogramming.

Who invented the Ruby language?

Yukihiro "Matz" Matsumoto, a Japanese computer programmer, invented this language in the mid-1990s.

What is the difference between the while loop and for loop in Ruby?

Both have similar functionality but have different syntax. Also for loop is preferred when we know how many times the loop will be executed.

Why for loop in Ruby is also known as an entry-controlled loop?

for loop is also called an entry-controlled loop because the condition is present at the beginning of the loop body.

Why a do-while loop is also known as an exit control loop?

It is also known as an exit control loop because the condition to be tested is at the last of the loop body. Even if the condition is false, the loop executes at least once.

Conclusion

This article discusses the for loop in Ruby with their syntax and some examples.

After reading about the for loop in Ruby, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See String literal in RubyHistory of RubyString Operators in RubyConstants in Ruby, and Hashes in Ruby to learn.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

conclusion image

Live masterclass