Loops are used when we have to perform an action over and over again. While writing a contract there may be a situation when we have to do some action repeatedly, In this situation, loops are implemented to reduce the number of lines of the statements. Solidity supports following loops too ease down the programming pressure.
While Loop
This is the most basic loop in solidity, Its purpose is to execute a statement or block of statements repeatedly as far as the condition is true and once the condition becomes false the loop terminates.
Syntax:
while (condition) {
statement or block of code to be executed if the condition is True
}
Example: In the below example, the contract Types demonstrate the execution of a while loop and how an array can be initialized using the while loop.
Solidity
pragma solidity ^0.5.0;
contract Types {
uint[] data;
uint8 j = 0;
function loop(
) public returns(uint[] memory){
while(j < 5) {
j++;
data.push(j);
}
return data;
}
}
|
Output :

Do-While Loop
This loop is very similar to while loop except that there is a condition check which happens at the end of loop i.e. the loop will always execute at least one time even if the condition is false.
Syntax:
do
{
block of statements to be executed
} while (condition);
Example: In the below example, the contract Types demonstrate the execution of a do-while loop and how an array can be initialized using the do-while loop.
Solidity
pragma solidity ^0.5.0;
contract Types {
uint[] data;
uint8 j = 0;
function loop(
) public returns(uint[] memory){
do {
j++;
data.push(j);
}while(j < 5) ;
return data;
}
}
|
Output :

For Loop
This is the most compact way of looping. It takes three arguments separated by a semi-colon to run. The first one is ‘loop initialization’ where the iterator is initialized with starting value, this statement is executed before the loop starts. Second is ‘test statement’ which checks whether the condition is true or not, if the condition is true the loop executes else terminates. The third one is the ‘iteration statement’ where the iterator is increased or decreased. Below is the syntax of for loop :
Syntax:
for (initialization; test condition; iteration statement) {
statement or block of code to be executed if the condition is True
}
Example: In the below example, the contract Types demonstrate the execution of a while loop and how an array can be initialized using the while loop.
Solidity
pragma solidity ^0.5.0;
contract Types {
uint[] data;
function loop(
) public returns(uint[] memory){
for (uint i=0; i<5; i++){
data.push(i);
}
return data;
}
}
|
Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 May, 2022
Like Article
Save Article