If / Else / For / While Loops
If Statements
If statements in Solidity look just like javascript.
Example - If / Else
For Loops
Sometimes you'll want to use a for loop to build the contents of an array in a function rather than simply saving that array to storage.
For our getZombiesByOwner
function, a naive implementation would be to store a mapping
of owners to zombie armies in the ZombieFactory
contract:
Then every time we create a new zombie, we would simply use ownerToZombies[owner].push(zombieId)
to add it to that owner's zombies array. And getZombiesByOwner
would be a very straightforward function:
The problem with this approach
This approach is tempting for its simplicity. But let's look at what happens if we later add a function to transfer a zombie from one owner to another. That transfer function would need to:
Push the zombie to the new owner's
ownerToZombies
array,Remove the zombie from the old owner's
ownerToZombies
array,Shift every zombie in the older owner's array up one place to fill the hole, and then
Reduce the array length by 1.
Step 3 would be extremely expensive gas-wise, since we'd have to do a write for every zombie whose position we shifted. If an owner has 20 zombies and trades away the first one, we would have to do 19 writes to maintain the order of the array.
Since writing to storage is one of the most expensive operations in Solidity, every call to this transfer function would be extremely expensive gas-wise. And worse, it would cost a different amount of gas each time it's called, depending on how many zombies the user has in their army and the index of the zombie being traded. So the user wouldn't know how much gas to send.hin
Of course, we could just move the last zombie in the array to fill the missing slot and reduce the array length by one. But then we would change the ordering of our zombie army every time we made a trade.
Since view functions don't cost gas when called externally, we can simply use a for-loop in getZombiesByOwner
to iterate the entire zombies array and build an array of the zombies that belong to this specific owner. Then our transfer
function will be much cheaper, since we don't need to reorder any arrays in storage, and somewhat counter-intuitively this approach is cheaper overall.
Using for loops
The syntax of
for
loops in Solidity is similar to JavaScript.Solidity supports
for
,while
, anddo while
loops.Don't write loops that are unbounded as this can hit the gas limit, causing your transaction to fail.
For the reason above, while and do while loops are rarely used.
Let's look at an example where we want to make an array of even numbers:
This function will return an array with the contents [2, 4, 6, 8, 10]
.
Example - For and While Loop
Last updated