Understanding continue
Loops are based on expressions. The logic of the expression decides the continuity of the loop. However, there are times when you are in between loop execution and want to go back to the first line of code without executing the rest of the code for the next iteration. The continue
statement helps us do that.
In the following code block, the for
loop is executed till the end; however, the values after 5
are not logged at all:
// SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; contract ForLoopExampleContinue { mapping (uint => uint) blockNumber; uint counter; event uintNumber(uint); function setNumber() public { blockNumber[counter++] = block.number; } function getNumbers() public { ...