Variables and control structures in solidity (100 Days of Solidity (Day 2–3) pt. 2)

Variables and control structures in solidity (100 Days of Solidity (Day 2–3) pt. 2)

Variables

In solidity, there are three types of variables. The state, the local, and the global

State variables

State variables are variables that store data on the blockchain. They are declared inside of a contract. The data stored in the variable will be stored on the blockchain.

Local Variable

They are variables whose values are available only within a function where they are defined. Function parameters are always local to that function. It only exists in the function when it is executed. After the function executes, the variable will be gone.

Below is an example of local and state variables in solidity.

Global variables

Here, information such as blockchain transactions is stored. These are special variables that exist in the global workspace and provide information about the blockchain and transaction properties.

Here are some global variables.

  • block.timestamp (unit): current block timestamp in seconds since Unix epoch

  • gasleft() returns (uint256): remaining gas

  • msg.data (bytes): complete calldata

  • msg.sender (address): sender of the message (current call)

  • msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)

  • msg.value (uint): number of wei sent with the message

  • tx.gasprice (uint): gas price of the transaction

  • tx.origin (address): sender of the transaction (full call chain)

  • assert(bool condition): abort execution and revert state changes if condition is false (use for internal error)

  • require(bool condition): abort execution and revert state changes if condition is false (use for malformed input or error in an external component)

  • require(bool condition, string memory message): abort execution and revert state changes if the condition is false (use for malformed input or an error in an external component). Also, provide an error message.

  • revert(): abort execution and revert state changes

Note that some of these global variables can have security implications if used incorrectly, so it’s important to use them carefully and with proper understanding.

The code above shows global variables and their use cases.

Solidity control structures

In solidity, we have control structures such as; if, else, while, do, for, break, continue, and return.

Parentheses cannot be omitted for conditionals, but curly braces can be omitted around single-statement bodies.

if-else

The syntax for writing if-else statements in Solidity is similar to Javascript. There is also a shorthand way to write the if-else statement. It is called the ternary operator. The ternary operator will be a better choice if you have a simple and short if-else statement.

for and while loops

Try to keep the number of iterations in your loop short. It will help save gas. In Solidity, we can use for and while loops to execute a block of code repeatedly.

An example of control structures being used in solidity is shown below.

Variables and control structures are important foundations of solidity.

In case you missed solidity data types, click here.

Click here to see the Github repo for this 100 days of solidity challenge.

Click here for guidelines for becoming a Solidity developer.

To learn more about blockchain, click here.

To learn more about Web3, click here.

Follow me for more knowledge on Solidity, Ethereum, and blockchain.