For Loops
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 31 of 53.
A for loop repeats a block while a condition remains true. Its header has three parts separated by semicolons: an initial value, a condition checked before each iteration, and an update after each iteration. i++ increases i by one.
The statements in this excerpt run inside the provided main function.
for (uint256 i = 1; i <= 3; i++) {
console.log(i);
}The counter starts at one. The loop prints it, increments it and checks the condition again. Once it becomes four, the condition is false. Keep loop bounds small in contract code because repeated work consumes execution resources.
A for-loop condition is checked before each iteration.
Challenge
EasyPrint each number from 1 through n, followed by done on its own line. n is between 0 and 8.
The tests pass arguments to main in this order: uint256 n. 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) 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