What are loops in Solidity?
We use loops when we want to perform an action repetitively. They make our code more organized and manageable.
Solidity is a language that supports the following loops used while creating smart contracts.
1. while loop
The while loop executes a block of code repeatedly, as long as the specified condition is true. When the condition becomes false, the loop will terminate.
If the condition is false at the start of the loop, it won’t execute the code.
Syntax
while (condition) {
// body
}
Example
In the following code snippet, we see the use of the while loop in Solidity:
pragma solidity ^0.5.0;contract Example {// declaring a state variableuint i = 0;// creating to function to use while loopfunction whileLoop() public returns(uint) {// creating a while loopwhile (i < 5) {i++;}// return ireturn i;}}
Explanation
-
Line 3: We create a contract named
Example. -
Line 5: We declare a state variable
i. -
Line 8: We create a function,
whileLoop. Inside this function, we create awhileloop (line 10) to increment the value ofiuntil the loop terminates. -
Line 15: We return the value of
i.
2. do-while loop
The do-while loop is similar to the while loop with the difference that it checks the condition at the end of the loop.
Therefore, it executes a block of code at least once, even if the condition is false.
Syntax
do {
// body
} while (condition);
Example
In the following code snippet, we see the use of the do-while loop in Solidity:
pragma solidity ^0.5.0;contract Example {// declaring a state variableuint i = 0;// creating to function to use do-while loopfunction doWhileLoop() public returns(uint) {// creating a do-while loopdo {i++;} while (i < 5);// return ireturn i;}}
Explanation
-
Line 3: We create a contract named
Example. -
Line 5: We declare a state variable,
i. -
Line 8: We create a function
doWhileLoop. Inside this function, we create ado-whileloop (line 10) to increment the value ofiuntil the loop terminates. -
Line 15: We return the value of
i.
3. for loop
The for loop also repeatedly executes a block of code. It accepts the three following arguments separated by a semi-colon (;).
loop initialization: This initializes the iterator. It is executed only once.loop condition: This checks whether or not the condition istrue. If the condition istrue, the body will be executed. If the condition isfalse, the loop will be terminated.loop iteration: This updates the iterator’s value. After updating the value, it will recheck the loop condition.
Syntax
for (initialization; condition; iteration) {
// body
}
Example
In the following code snippet, we see the use of the for loop in Solidity.
pragma solidity ^0.5.0;contract Example {// declaring a state variableuint i = 0;// creating to function to use for loopfunction forLoop() public returns(uint) {// creating a for loopfor (uint j = 0; j < 5; j++) {i++;}// return ireturn i;}}
Explanation
-
Line 3: We create a contract named
Example. -
Line 5: We declare a state variable,
i. -
Line 8: We create a function,
forLoop. Inside this, we create aforloop (line 10) to increment the value ofiuntil the loop terminates. -
Line 15: We return the value of
i.