Back
Lesson 14:
 Loops
Introduction to loops (for/while) and how to use it in your smart contracts.
Progress: 0%
Visit desktop version for better experiences.
Loops
For/While
Loops are used to repeat a block of code multiple times. There are three types of loops in Solidity: for, while and do-while loops.
Note: Loops with unfixed iterations can cause transaction failure due to block gas limits, which is why while and do-while are rarely used.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Loops {
    // 'for' loop example
    function sumUpTo(uint256 n) public pure returns (uint256) {
        uint256 sum = 0;
        for (uint256 i = 0; i <= n; i++) {
            sum += i;
        }
        return sum; // Returns the sum of all numbers up to 'n'
    }
    // 'while' loop example
    function countDownFrom(uint256 n) public pure returns (uint256[] memory) {
        uint256[] memory countdown = new uint256[](n);
        while (n > 0) {
            countdown[n-1] = n;
            n--;
        }
        return countdown; // Returns an array counting down from 'n' to 1
    }
    // 'do-while' loop example (use with caution due to potential for infinite loops)
    function doWhileLoop(uint256 n) public pure returns (uint256) {
        uint256 i = 0;
        do {
            i++;
        } while (i < n);
        return i; // Returns 'n'
    }
}