Let's learn about loops. Loops are very useful for iterating through certain parts of our code until certain conditions are met. Let's explore how they work in Solidity. And we're going to extend our previous contract and create a function called loop that will be external. First, let's initialize a for loop.
And the way we declare a for loop in Solidity is very similar to other programming languages, where we specify `for`, then we do the initialization, then we specify a condition, and finally, we define how the counter increments. So, in Solidity, we simply say `for` and declare a counter that we'll call `index`. So, the index will start at zero. And then we say that we will execute this part of the code until index is less than eight. OK?
And then we say that we want to increment the index one by one. All good. So, here we're going to register and we're going to use a smart contract that Remix provides. Which is called Hardhat console.sol.
By adding this import, we will be able to basically say the index is and then pass the index value. OK? So, what we're going to do is call the loop. The loop function will trigger this for loop loop function will trigger this for loop . And then we'll record the value of the index variable.
And let's see how it increases, because here we are increasing one by one, indexing more and more. Okay, let's test it. We implemented the contract and now we call the loop. Okay, and as you can see, we are recording the index value. So, the first time it starts at zero, it increases one by one.
And when the value is seven, it will be executed for the last time. But note, when the value is eight, it will no longer be executed, because index, let's say the value is equal to eight, eight is less than eight, that's false. Since this condition is not true Since this condition is not true , the loop execution stops. Now, let's add a little more logic. What we're going to do here is add an if statement.
And we'll say if index is two, we simply say console.log index is two. And here we're going to introduce a keyword called continue. OK? The "continue" button will essentially cause you to jump to the next cycle.
So, for example, if I have a console.log here called if I have a console.log here called , let's say, `then index equals two`. Because when the index is two, it will enter this block. And then it will execute the continue, which means it will jump to the next cycle.
OK? Therefore, he will not record this part. However, when the value is, say, one, it won't go into this section, so it will only record this part. OK? And then we'll include another if statement.
And we 'll say, if the index is four, we'll record the index as four. But here, what we're going to do is use the keyword break. This means that the execution of the for loop will terminate because of the break statement. Okay, so we're basically going to get out of this loop. But let's add a log here and call it the end of the loop.
So, the expectation here is that when the index is zero, one, two, and three, it will register that. However three, it will register that. However , as soon as it reaches the value four, it will enter here and interrupt the for loop, which means we will no longer see this message. OK? So, let's test this example.
We will implement the contract. And now let's call the loop. Okay, let's analyze the results. The first thing to note is that the index is zero when we run it for the first time. So he can't come in here because he's a zero.
He'll go straight to this one. And then he doesn't come in here. Therefore, it goes directly to the end of the loop. Then we enter again when the index is one. Again, this part.
And again, the end of the loop. However, when the index is two, as you can see, we enter here. And we say can see, we enter here. And we say , the index is two. OK?
But because we are executing the continue loop, the rest of the code is ignored. So rest of the code is ignored. So , we move on to the next cycle. And as you can see, after the index is two, we simply move on to this part. The index is three.
So, we'll skip this part. We are registering this again. And that's the end of the loop. However, when the index is four, as you can see, we don't enter here as you can see, we don't enter here . We completed this part.
But now, we've entered this field. Here we record that the index is four, but we interrupt using the break keyword and immediately exit the loop. So, even when the condition says that the loop should run while the index is less than eight, because of this condition and the break, we basically exit the for loop.
Automatic transcript β names and jargon may be misspelled.