Loops
Let's have a look at the setInitialOwners
function on line 85. Here we find a new type of control flow, a "for loop".
function setInitialOwners(address[] addresses, uint[] indices) { uint n = addresses.length; for (uint i = 0; i < n; i++) { setInitialOwner(addresses[i], indices[i]); }}
The for-loop itself reads as follows:
i
is zero- as long as
i
is less thann
, run the code within the for loopsetInitialOwner(...)
- after each iteration, increment
i
by 1 - if
i
is equal or greater thann
, exit the loop and continue with the code below the loop section
The function expects two arguments, each containing a list of items. They would look something like this:
addresses = [0x01234, 0x56789, 0xabcde];indices = [100, 101, 102];
If we were to call the function with these inputs, we can imagine the flow of this code like this:
setInitialOwners(addresses, indices) // setInitialOwners( // [0x01234, 0x56789, 0xabcde], // [100, 101, 102] // );uint n = addresses.length; // n = 3uint i = 0; // i = 0// First Iteration...i < n; // 0 < 3 = truesetInitialOwner(addresses[i], indices[i]) // setInitialOwner(0x01234, 100)i++; // i = 1// Second Iteration...i < n; // 1 < 3 = truesetInitialOwner(addresses[i], indices[i]) // setInitialOwner(0x56789, 101)i++; // i = 2// Third Iteration...i < n; // 2 < 3 = truesetInitialOwner(addresses[i], indices[i]) // setInitialOwner(0xabcde, 102)i++; // i = 3// Exit Loopi < n; // 3 < 3 = false
You've gotten so far – congratulations! Code might look intimidating at first, but if we spend the time to go through it step by step it turns out it's not at all that bad... We now know all the fundamentals we needed before diving in to the actual functionality.