Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Basic For Loop
  • For Loop with Multiple Variables
  • For Loop with Break Statement
  1. STEP 2

15: For Loop

A for loop is a type of loop that is used in Solidity to execute a set of statements repeatedly until a certain condition is met. The syntax for a for loop is as follows:

for (init; condition; increment/decrement) {    
    // Statements to be executed
}

init: This is an expression that is used to initialize the loop. It is usually an assignment statement.

condition: This is the condition that is used to determine whether the loop should be executed or not. If the condition is true, the loop will execute. If the condition is false, then the loop will not execute.

increment/decrement: This is an expression that is used to increment or decrement the loop counter.

For example, the following code will print the numbers from 0 to 9:

for (uint i = 0; i < 10; i++) {    
    // Print the current value of 'i'    
    Console.log(i); // console log - component of hardhat
}

This code will print the numbers from 0 to 9 to the console.

Basic For Loop

The following is an example of a basic for loop:

for (uint i = 0; i < 10; i++) {   // Do something}

This code will execute the statements inside the code block 10 times, starting from 0 and incrementing i each iteration.

For Loop with Multiple Variables

For loops can also be written with multiple variables. This can be done by separating the variables with a comma:

for (uint i = 0, uint j = 10; i < 10; i++, j--) {   // Do something}

This code will execute the statements inside the code block 10 times, starting from 0 and incrementing i and decrementing j each iteration.

For Loop with Break Statement

For loops can also be written with a break statement. This can be done by using the break keyword to exit the loop at a certain point.\ For example:

for (uint i = 0; i < 10; i++) {   
    // Do something   
    if (i == 5) {      
        break;   
    }
}

This code will execute the statements inside the code block 10 times, starting from 0 and incrementing i each iteration. The loop will exit the loop when i reaches 5.

That's it for the lesson 15! In the next lesson, Required

Previous14: Do While LoopNext16: Required

Last updated 2 years ago