Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct

Solidity Crash Course – Part 05 🚀

Introduction 👋

Welcome back to our Solidity crash course! In this part, we’ll cover some essential concepts to help you write more efficient and structured smart contracts. Let’s dive in! 🏊‍♂️


This content originally appeared on DEV Community and was authored by Muhammad Ayaz

Solidity Crash Course - Part 05 🚀

Introduction 👋

Welcome back to our Solidity crash course! In this part, we'll cover some essential concepts to help you write more efficient and structured smart contracts. Let's dive in! 🏊‍♂️

Solidity - While Loops 🔄

Loops are used to execute a block of code multiple times based on a condition. A while loop continues to execute as long as the condition remains true.

Example:

pragma solidity ^0.8.0;

contract WhileLoopExample {
    uint public count;

    function countUp(uint _limit) public {
        while (count < _limit) {
            count++;
        }
    }
}

Why use while loops?

  • Great for situations where you don’t know the exact number of iterations beforehand.

⚠️ Best Practice: Be cautious of infinite loops as they can waste gas and cause transactions to fail.

Solidity - Events 📢

Events allow smart contracts to communicate with external applications like dApps. They help log important actions that occur on the blockchain.

Example:

pragma solidity ^0.8.0;

contract EventExample {
    event NumberChanged(uint oldValue, uint newValue);
    uint public number;

    function updateNumber(uint _newValue) public {
        emit NumberChanged(number, _newValue);
        number = _newValue;
    }
}

Why use events?

  • Efficiently store data without consuming unnecessary gas.
  • Helps external applications listen to changes.

⚠️ Best Practice: Use events to log only important updates to reduce gas costs.

Solidity - For Loops 🔁

A for loop is used when you know the number of iterations beforehand.

Example:

pragma solidity ^0.8.0;

contract ForLoopExample {
    uint[] public numbers;

    function fillArray(uint _limit) public {
        for (uint i = 0; i < _limit; i++) {
            numbers.push(i);
        }
    }
}

Why use for loops?

  • Ideal when you need to iterate a known number of times.

⚠️ Best Practice: Avoid looping through large arrays to prevent exceeding gas limits.

Solidity - Arrays 📦

Arrays store multiple values in a single variable, allowing efficient data management.

Example:

pragma solidity ^0.8.0;

contract ArrayExample {
    uint[] public numbers;

    function addNumber(uint _num) public {
        numbers.push(_num);
    }

    function getNumber(uint index) public view returns (uint) {
        return numbers[index];
    }
}

Why use arrays?

  • Efficient storage and retrieval of multiple data points.
  • Useful for managing lists like registered users or transaction records.

⚠️ Best Practice: Use mappings instead of arrays when searching for values frequently.

Solidity - Struct 🏗️

Structs allow you to create custom data types, grouping multiple properties together.

Example:

pragma solidity ^0.8.0;

contract StructExample {
    struct Person {
        string name;
        uint age;
    }

    Person public user;

    function setUser(string memory _name, uint _age) public {
        user = Person(_name, _age);
    }
}

Why use structs?

  • Helps organize related data efficiently.
  • Useful for defining complex objects like user profiles or product details.

⚠️ Best Practice: Keep struct sizes minimal to optimize gas usage.

Conclusion 🎯

We covered essential Solidity concepts like loops, events, arrays, and structs. These are fundamental for writing more structured and efficient smart contracts.

Let me know if you have any questions, comments, or feedback! Would love to help you out! 😊


This content originally appeared on DEV Community and was authored by Muhammad Ayaz


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Ayaz | Sciencx (2025-02-01T13:28:58+00:00) Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct. Retrieved from https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/

MLA
" » Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct." Muhammad Ayaz | Sciencx - Saturday February 1, 2025, https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/
HARVARD
Muhammad Ayaz | Sciencx Saturday February 1, 2025 » Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct., viewed ,<https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/>
VANCOUVER
Muhammad Ayaz | Sciencx - » Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/
CHICAGO
" » Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct." Muhammad Ayaz | Sciencx - Accessed . https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/
IEEE
" » Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct." Muhammad Ayaz | Sciencx [Online]. Available: https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/. [Accessed: ]
rf:citation
» Solidity Crash Course – Part 05: While,For loop, Event, Arrays and Struct | Muhammad Ayaz | Sciencx | https://www.scien.cx/2025/02/01/solidity-crash-course-part-05-whilefor-loop-event-arrays-and-struct/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.