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.
while
loopThe 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.
while (condition) {
// body
}
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;}}
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 a while
loop (line 10) to increment the value of i
until the loop terminates.
Line 15: We return the value of i
.
do-while
loopThe 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
.
do {
// body
} while (condition);
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;}}
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 a do-while
loop (line 10) to increment the value of i
until the loop terminates.
Line 15: We return the value of i
.
for
loopThe 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 is true
. If the condition is true
, the body will be executed. If the condition is false
, the loop will be terminated.loop iteration
: This updates the iterator’s value. After updating the value, it will recheck the loop condition.for (initialization; condition; iteration) {
// body
}
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;}}
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 a for
loop (line 10) to increment the value of i
until the loop terminates.
Line 15: We return the value of i
.