For Loop in Solidity
Basic For Loops in Solidity
For Loops in almost all languages are similar to each other. It's the same case for Solidity as well! A For Loop functions in Solidity the same way as it would in Java, Javascript or Python. A for loop is the most used and the easiest way to loop over anything in your code. It takes three arguments each separated with semi-colons. The first argument is initializing where you want to start your loop from, the second argument takes a statement and basically check whether the statement you have entered is true or false, if it is true the for loop is executed or else it is terminated, the third argument is what you want to do after the loop is executed which means that whether you want to increment or decrement the value after the for loop is executed.
This is the basic syntax of how to write a For Loop
for (initialization, test statement, increment/decrement) {
block of code which is to be executed if the statement is true
}
Let's take a look at how to write a For Loop in Solidity:
pragma solidity ^ 0.8.4
contract forLoop {
uint [] values;
function looping () public returns (uint[] memory) {
for (uint i = 0; i < 5; i++){
values.push(i)
}
return data;
}
}
If you have any questions, you can find me on my Twitter and LinkedIn