Understanding breaks
Loops help to iterate over from the start till they arrives on a vector data type. However, there are times when you might want to stop the iteration in between and jump out or exit from the loop without executing the conditional test again. The break
statement helps us do that. It helps us terminate the loop by passing the control to the first instruction after the loop.
In the following code example, the for
loop is terminated and control moves out of the for
loop when the value of i
is 1
because of the use of the break
statement. It literally breaks the loop, as shown in the following code block:
// SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; contract ForLoopExampleBreak { mapping (uint => uint) blockNumber; uint counter; event uintNumber(uint); function setNumber() public { ...