Solidity – Loops

In Solidity, loops are used to repeat a block of code a certain number of times. Solidity supports different types of loops:

  1. For Loop: The for loop is used to execute a block of code a specified number of times. The basic syntax for a for loop is:
for (initialization; condition; increment) {
    // code to be executed
}

Here’s an example of using a for loop in Solidity:

pragma solidity ^0.8.0;
contract Example {
    function forLoop() public {
        for (uint i = 0; i < 10; i++) {
            // code to be executed
        }
    }
}

In this example, the for loop will execute the code inside the loop 10 times, starting from i = 0, and incrementing by 1 each time.

  1. While Loop: The while loop is used to execute a block of code repeatedly as long as a given condition is true. The basic syntax for a while loop is:
while (condition) {
    // code to be executed
}

Here’s an example of using a while loop in Solidity:

pragma solidity ^0.8.0;
contract Example {
    uint a = 0;
    function whileLoop() public {
        while (a < 10) {
            a++;
            // code to be executed
        }
    }
}

In this example, the while loop will execute the code inside the loop until the value of a is greater than or equal to 10.

3. Do-While Loop: Solidity also supports do...while loops, which are similar to while loops but with one key difference: the code inside the loop is guaranteed to execute at least once, regardless of whether the condition is true or false.

The basic syntax for a do...while loop is:

do {
    // code to be executed
} while (condition);

Here’s an example of using a do...while loop in Solidity:

pragma solidity ^0.8.0;
contract Example {
    uint a = 0;
    function doWhileLoop() public {
        do {
            a++;
            // code to be executed
        } while (a < 10);
    }
}

Loop Control

Solidity does not support the traditional break and continue statements for controlling the flow of a loop. However, there are a few ways to achieve similar functionality in Solidity:

  1. Boolean flag: One way to break out of a loop in Solidity is to use a Boolean flag variable. Before the start of the loop, initialize a Boolean flag variable to false. Inside the loop, check the flag variable before each iteration, and if it’s set to true, use a return statement to exit the function. Inside the loop, when the desired condition is met, set the flag variable to true, and the loop will exit on the next iteration.
pragma solidity ^0.8.0;
contract Example {
    function breakExample() public {
        bool flag = false;
        for (uint i = 0; i < 10; i++) {
            if (flag) {
                return;
            }
            if (i == 5) {
                flag = true;
            }
        }
    }
}
  1. Labeled Break: If you need to break out of multiple nested loops, you can use labeled break statement.
pragma solidity ^0.8.0;
contract Example {
    function breakExample() public {
        outer: for (uint i = 0; i < 10; i++) {
            for (uint j = 0; j < 10; j++) {
                if(j == 5) {
                    break outer;
                }
            }
        }
    }
}
  1. Return: Another way to break out of a loop in Solidity is to use the return statement. When the desired condition is met, use a return statement to exit the function and return a value if necessary.
pragma solidity ^0.8.0;
contract Example {
    function breakExample() public {
        for (uint i = 0; i < 10; i++) {
            if(i == 5) {
                return;
            }
        }
    }
}

It’s worth noting that, as with loops in general, it’s important to be mindful of the gas cost when using these techniques to control the flow of a loop, and also to consider alternative ways of achieving the same result, like using a for loop with a pre-calculated number of iterations.

Here’s an example of how you might simulate a break and continue statement in Solidity:

pragma solidity ^0.8.0;
contract Example {
    function example() public {
        for (uint i = 0; i < 10; i++) {
            if (i == 5) {
                break;
            }
            if (i % 2 == 0) {
                continue;
            }
            // code to execute
        }
    }
}

In this example, we’re using a for loop to iterate through a range of numbers. Inside the loop, we’re using an if statement to check the value of i. If i is equal to 5, we’re using a break statement to exit the loop, otherwise, we’re checking if i is even. If i is even we’re using a continue statement to skip the current iteration of the loop and move on to the next iteration.

It’s important to note that, this code is not actually using break and continue statements, but rather using a combination of conditional statements, which can help achieve a similar functionality.

It’s also important to be mindful of the gas cost when using these techniques to control the flow of a loop, and also to consider alternative ways of achieving the same result, like using a for loop with a pre-calculated number of iterations.

close button
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
AdBlock Detected

Looks like you are using an ad-blocking browser extension. We request you to whitelist our website on the ad-blocking extension and refresh your browser to view the content.

0
Would love your thoughts, please comment.x
()
x