> For the complete documentation index, see [llms.txt](https://hunter-ducharme.gitbook.io/ruby-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hunter-ducharme.gitbook.io/ruby-programming/ruby_loops.md).

# Ruby Loops

### Table of Contents

1. `for` loops
2. `while` loops
3. `until` loops
4. `loop` loops
5. `.times` iterator loop

## A 'for' loop:

```ruby
for i in 1..50
    puts "#{i}"
end
```

1. For every number between 1 - 50
2. Using the inclusive `..` operator tells the computer to include the last number in the loop
3. Will `puts` numbers 1 - 50
4. Using the exclusive `...` operator tells the computer to exclude the last number from the loop
5. Will `puts` the numbers 1-49
6. Display the number on the console
7. Ends instructions for the loop

## A 'while' loop:

```ruby
i = 1
while i <= 50
    puts "#{i}"
    i += 1
end
```

1. Sets variable `i` equal to one
2. While `i` is less than or equal to 50, do the block of code (will print numbers 1 -50)
3. Display the number on the console
4. Increment `i` by one every time, that way the loop will end at some point
5. Ends instructions for the loop

## An 'until' loop:

```ruby
i = 1
until i > 50
    puts "#{i}"
    i += 1
end
```

1. Sets variable `i` equal to one
2. Until `i` is greater than 50, run the block of code (will print numbers 1-50)
3. Display `i` on the console
4. Increment `i` by one every time, that way the loop will end at some point
5. End the instructions for the loop

## A 'loop' loop:

```ruby
i = 1
loop do
    puts "#{i}"
    i += 1
    break if i > 50
end
```

1. Sets variable `i` equal to one
2. Create a `loop`, and run the block of code
3. Display `i` on the console
4. Increment `i` by one every time, that way the loop will end at some point
5. Break the loop if `i` becomes greater than 50
6. End the instructions for the loop

## A '.times' iterator loop:

```ruby
5.times do
    puts "Hello world!"
end
```

1. Run this loop five times, and do the block of code
2. Display the string on the console
3. End the instructions for this loop


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hunter-ducharme.gitbook.io/ruby-programming/ruby_loops.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
