**Unlocking the Power of Blockchain: Solidity Mastery: โ€” Part 3**

ยท

5 min read

๐ŸŒŸ Introduction to Solidity: Embrace the Power of Smart Contracts๐ŸŒŸ

Discover the fascinating world of Solidity, an object-oriented, high-level language that breathes life into smart contracts on the Ethereum Virtual Machine (EVM). Letโ€™s embark on a journey of innovation and explore the limitless possibilities it offers for decentralized applications (DApps) and trustless transactions on the blockchain.

๐Ÿ“š Variables and Types: The Building Blocks of Blockchain ๐Ÿ“š

In the realm of Solidity, variables play a crucial role in shaping the fate of smart contracts. Delve into three unique types: local, state, and global.

Local

Declared inside a function and are not stored on blockchain

State

Declared outside a function to maintain the state of the smart contractStored on the blockchain

Global

Provide information about the blockchain. They are injected by the Ethereum Virtual Machine during runtime. Includes things like transaction sender, block timestamp, block hash, etc.

What are state variables??

State variables are variables that are stored permanently on the blockchain. They are used to maintain the state of a blockchain application, such as the balance of an account, the ownership of an asset, or the status of a contract.

State variables are declared in smart contracts, which are the programs that run on the blockchain. When a smart contract is deployed, its state variables are created and stored on the blockchain. Any changes to the state variables are recorded in the blockchainโ€™s ledger.

๐Ÿš€ Solidity Code: A Hands-On Adventure ๐Ÿš€

Code:

// Define the compiler version you would be using
pragma solidity ^0.8.19;

// Start by creating a contract named Variables
contract Variables {
    /*
        ******** State variables **********
    */
    /*
    uint stands for unsigned integer, meaning non negative integers
    different sizes are available. Eg
        - uint8   ranges from 0 to 2 ** 8 - 1
        - uint256 ranges from 0 to 2 ** 256 - 1
    `public` means that the variable can be accessed internally
     by the contract and can also be read by the external world
    */
    uint8 public u8 = 10;
    uint public u256 = 600;
    uint public u = 1230; // uint is an alias for uint256

    /*
    Negative numbers are allowed for int types. Eg
    - int256 ranges from -2 ** 255 to 2 ** 255 - 1
    */
    int public i = -123; // int is same as int256

    // address stands for an ethereum address
    address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;

    // bool stands for boolean
    bool public defaultBoo1 = false;

    // Default values
    // Unassigned variables have a default value in Solidity
    bool public defaultBoo2; // false
    uint public defaultUint; // 0
    int public defaultInt; // 0
    address public defaultAddr; // 0x0000000000000000000000000000000000000000

    function doSomething() public {
        /*
        ******** Local variable **********
        */
        uint ui = 456;

        /*
        ******** Global variables **********
        */

        /*
            block.timestamp tells us whats the timestamp for the current block
            msg.sender tells us which address called the doSomething function
        */
        uint timestamp = block.timestamp; // Current block timestamp
        address sender = msg.sender; // address of the caller
    }
}

๐Ÿ›๏ธ Functions, Loops, and If/Else ๐Ÿ›๏ธ

๐Ÿ›๏ธ Building Intelligent Contracts Snippet # 2 ๐Ÿ›๏ธ

// Define the compiler version you would be using
pragma solidity ^0.8.19;

// Start by creating a contract named Conditions
contract Conditions {
    // State variable to store a number
    uint public num;

    /*
        Name of the function is set.
        It takes in an uint and sets the state variable num.
        It is declared as a public function meaning
        it can be called from within the contract and also externally.
    */
    function set(uint _num) public {
        num = _num;
    }

    /*
        Name of the function is get.
        It returns the value of num.
        It is declared as a view function meaning
        that the function doesn't change the state of any variable.
        view functions in solidity do not require gas.
    */
    function get() public view returns (uint) {
        return num;
    }

    /*
        Name of the function is foo.
        It takes in an uint and returns an uint.
        It compares the value of x using if/else
    */
    function foo(uint x) public returns (uint) {
        if (x < 10) {
            return 0;
        } else if (x < 20) {
            return 1;
        } else {
            return 2;
        }
    }

    /*
        Name of the function is loop.
        It runs a loop till 10
    */
    function loop() public {
        // for loop
        for (uint i = 0; i < 10; i++) {
            if (i == 3) {
                // Skip to next iteration with continue
                continue;
            }
            if (i == 5) {
                // Exit loop with break
                break;
            }
        }
    }


}

โ›๏ธ Unearthing the Blockchain: Do Your Own Research! โ›๏ธ

Empower yourself to seek knowledge beyond the surface. Uncover the answer to intriguing questions like โ€œWhat is the value of block.coinbase?โ€ Sharpen your understanding of blockchain concepts, and witness how Solidity intertwines seamlessly with Ethereum.

๐Ÿ”“ Unlock the Secrets of Solidity: Embrace the Future of Decentralized Technology ๐Ÿ”“

Are you ready to unleash the full potential of Solidity and Ethereum? Together, weโ€™ll embark on a thrilling journey of smart contract development, unlocking the door to decentralized innovation. Join us as we shape the future of blockchain technology! Letโ€™s connect and explore opportunities together! ๐Ÿค๐Ÿš€

Special thanks to learnweb3.io, cryptozombies, and simplilearn.com for providing valuable content that contributed to the creation of this article.

#Blockchain #Solidity #Smart Contracts #Cryptocurrency #Decentralizedapplication
ย