> For the complete documentation index, see [llms.txt](https://move-developers-dao.gitbook.io/aptos-move-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://move-developers-dao.gitbook.io/aptos-move-by-example/basic-concepts/loops.md).

# Loops

**Loops:**

Move provides three ways for defining loops:

* using `while`
* using  `loop`
* using `for`&#x20;

```rust
module my_addrx::Loops
{
    //lets find the sum of first N natural numbers using while and infinite loop

    //using while loop
    fun sum_using_while(n:u64) :u64
    {
        let sum=0;
        let i:u64=1;  //setting counter to 1
        while(i <= n) //This Loop will run until the expression inside the round brackets is valid
        {
            sum=sum+i;
            i=i+1;  //incrementing the counter
        }; //while is an expression therefore it should be end with a semicolon.
        sum //you can also return in functions like this.
    }

    //using for loop
    fun sum_using_for(n:u64) :u64
    {
        let sum=0; 
        for (i in 1..(n+1)) //range -> from 1 to n
        {
            sum=sum+i; 
        }; //for is an expression therefore it should be end with a semicolon.
        sum //you can also return in functions like this.
    }

    //using infinite loop
    fun sum_using_loop(n:u64) :u64
    {
        let sum=0;
        let i:u64=1;
        loop
        {
            sum=sum+i;
            i=i+1;
            if(i>n)
                break;   //break statement terminates the loops 
        };
        sum
    }

    #[test_only]
     use std::debug::print;
    #[test]
    fun testing()
    {
        let sum = sum_using_while(10);
        print(&sum);
        let sum = sum_using_loop(10);
        print(&sum);
        let sum = sum_using_for(10);
        print(&sum);
    }
}
```

{% hint style="info" %}
Similar to `break` statement there is `continue` statement which forces the program to execute next iteration by skipping the current one.
{% endhint %}


---

# 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://move-developers-dao.gitbook.io/aptos-move-by-example/basic-concepts/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.
