# 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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
