Break and Continue
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 34 of 53.
Inside a loop, break exits the loop immediately. continue skips the rest of the current iteration and moves to the next iteration. In a for loop the update expression still runs after continue.
The statements in this excerpt run inside the provided main function.
for (uint256 i = 1; i <= 5; i++) {
if (i == 2) { continue; }
if (i == 4) { break; }
console.log(i);
}The second iteration skips printing. The fourth exits before printing, so five is never reached. Break and continue affect the nearest enclosing loop, not every function in the contract.
break exits the nearest loop; continue skips the rest of the current iteration.
Challenge
EasyPrint integers from 1 through n, skipping the supplied banned value, then print done. n is at most 8 and banned is between 1 and 10.
The tests pass arguments to main in this order: uint256 n, uint256 banned. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 n, uint256 banned) external view {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground