▫️Lesson 2.2: Solidity Variables, Types, and Expressions

Module 2: Developing Smart Contracts with Solidity

Objective

To deepen your understanding of Solidity by exploring its variables, data types, and expressions. This lesson will cover how to declare variables, the types of variables available in Solidity, and how to use expressions to manipulate data within smart contracts.


Variables in Solidity

Variables in Solidity are declared as either state variables, local variables, or global variables.

  • State Variables: Stored on the blockchain, persist across function calls and transactions.

  • Local Variables: Exist only within the function context and not stored on the blockchain.

  • Global Variables: Provide information about the blockchain and are available to all smart contracts.

Example:

pragma solidity ^0.8.0;

contract VariablesExample {
    // State variable
    uint public stateVariable;

    function exampleFunction() public {
        // Local variable
        uint localVariable = 45;

        // Global variable (block timestamp)
        uint timestamp = block.timestamp; // Current block timestamp
    }
}

Data Types in Solidity

Solidity supports various data types, including:

  • Value Types: Include boolean, integer, address, and fixed-point numbers. These are always passed by value.

  • Reference Types: Include arrays, structs, and mappings. These can be stored in storage or memory and are passed by reference.

  • Special Types: Such as bytes, enum, and contract types.

Example:

pragma solidity ^0.8.0;

contract DataTypesExample {
    bool public booleanValue = true;
    int public integerValue = -100;
    uint public unsignedInteger = 100; // uint is an alias for uint256
    address public contractAddress = address(this);
    string public stringValue = "Hello, Solidity!";
}

Expressions and Control Structures

Solidity supports control structures similar to those in JavaScript, including if-else statements, for loops, while loops, and do-while loops. Expressions in Solidity allow for the manipulation of data through operations such as addition, subtraction, multiplication, and division.

Example:

pragma solidity ^0.8.0;

contract ControlStructuresExample {
    uint public count;

    function increment(uint _value) public {
        if (_value > 0) {
            count += _value;
        }
    }
}

Activity: Experimenting with Data Types and Control Structures

  1. Create a new contract: Use Remix IDE to create a contract that demonstrates the use of different data types and control structures.

  2. Implement a function: Write a function that uses if-else statements to perform different arithmetic operations based on input parameters.

  3. Test your contract: Deploy your contract in Remix and test the function with various inputs to observe the outcomes.


Exercise

Practical Exercise:

  • Extend the ControlStructuresExample contract by adding a function that decrements the count variable by a specified value, but never lets it go below 0. Use control structures to ensure count cannot become negative.


This lesson has expanded your knowledge of Solidity by introducing variables, data types, and control structures. Understanding these concepts is crucial for writing efficient and effective smart contracts.

Last updated