Solidity For Loop

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

For Loops create the ability to loop through a certain section of code multiple times, creating a simple way to automate certain code. They should be used lightly in Solidity, since they are very computationally heavy and usually require a lot of gas to run.

Here is a very simple “for loop” written in solidity that simply continues to add one to the variable named “counter” until the loop is finished:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;


contract loop{

    uint public number = 0;

    function doTheLoop() public{

        uint numberOfLoops = 10;

        for(uint i = 0; i < numberOfLoops; i++){
            number++;
        }

    }

}

If you compile this contract in Remix, and then deploy it on the virtual machine they host, you’ll see that there is a function you can call and a variable value you can get:

for loop written in Solidity in the remix IDE

If you click on the “number” button, it will get the value of this variable on the blockchain. In this case, since we initialized it at 0, it is still zero.

However, if we click on the “doTheLoop” button, it will run the “doTheLoop” function, which includes a for loop and increases the value of the variable number by 1.

Since this loop is set up to run 10 times, the first time we run this fuction ,the value of “number” will end at 10. If we run the function again, it will be increase to 20, and so on.

Let’s break down each line of this for loop:

for(uint i = 0; i < numberOfLoops; i++){

This line of code initializes the for loop. There is a lot to break down here, andif you look closely, there are 3 parts to this, separated by semicolons. Let’s go slowly:

  1. uint i = 0; – This part creates a new temporary variable “i” that is of the type “uint” and has the value 0. This variable can be called inside the loop at any time, and will change throughout the loop. In many cases, this variable is a counter to keep track of how many times the loop has been ran.
  2. i < numberOfLoops; -This second part of the loop is the conditional to check if the loop will run again. If this is true, the loop will continue. In this case, we are comparing our temporary loop variable “i” to see if it is less than the variable “numberOfLoops”, which was created outside of the variable and set to 10.
  3. i++ – This is a simple way of saying “add one to the variable i”. In short, this piece of code runs after the loop runs. In this case, it updates the value of “i” by one number, and then will check the conditional again to see if it should run again.

Hopefully that made it easier to understand how for loops work in Solidity. In fact, they work very similar to any other language, the syntax may just differ. Many senior developers can use for loops in ways novices wouldn’t expect. For example, you can skip every item in an array by starting at 0 and then increasing the value of “i” by 2 each iteration. In other cases, you can start at a very high number, and then subtract from “i” to head backwards.

After the end of the line, we start our loop procedure with a curly bracket. Anything after the curly bracket, and before the associated closing curly bracket will be ran each loop.

number++;

This line is very easy to understand, it just adds one value to the current value of the variable “number”. Remember, “number++;” is just a simpler way of writing “number = number + 1;”.

Finally, at the end, we have our ending curly bracket, since this is a very simple for loop.

A very similar function in Solidity is something called a “while loop” or a “do while loop”. These are very rarely used in Solidity for many reasons.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.