For, break, and continue
Filter loop iterations with continue and stop the loop with break.
Solidity 0.8.36REPL mode
Why use this example
Use this pattern to isolate Solidity branching, looping, assignment, and function-call syntax before combining it with contract state. The entry isolates for, break, and continue so its input, result, and state changes stay easy to compare.
Solidity to run
function sumOddBelowSeven() public pure returns (uint256 total) {
for (uint256 i = 0; i < 10; i++) {
if (i == 7) break;
if (i % 2 == 0) continue;
total += i;
}
}
sumOddBelowSeven()Official Solidity reference
Compare this focused investigation with the complete language definition in the Solidity documentation.
Control structuresWhat to expect
The loop skips even values, stops at seven, and returns the sum of 1, 3, and 5 as 9.
Continue the investigation
Change a value, add another expression, or open Inspect after the run. Accepted entries remain available to the next entry, and the clean replay keeps the complete result reproducible.
Read about writing Solidity Prototype Solidity behavior