Solidity Tutorial

If Else Conditional Statements in Solidity

Share on twitter
Share on linkedin
Share on whatsapp
Share on facebook
Share on telegram

What are Conditional Statements

In programming, there is always a need to write conditional logic such that if certain conditions are met then a specific piece of code is executed, else another piece of code is executed. Such conditional statements are incorporated by using If… Else statements. All programming languages have If… Else or variation of it.

if else conditional statements in Solidity
(Source)

 

Conditional Statements in Solidity

In Solidity, there are three types of conditional statements as follows –

  • if Statement
  • if… else Statement
  • if… else if Statement

Let us understand each of these solidity conditional statements more in detail.

if Statement – Solidity

The if statement is the basic decision control statement in Solidity. If the conditions of if statement are fulfilled then the execution of the code will go inside the if block else it will skip the if block.

Below is the syntax of the if statement in Solidity.

if (conditions)
{
  // Code inside this block is executed when if conditions are True
}

Example

In the below example, inside the set function, the state variable storedData is multiplied by 0 if its value is less than 50.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;


contract DemoContract 
{
    uint storedData;

    function set(uint x) public 
    {
        storedData = x;

        if (storedData < 50)
        {
            storedData = storedData*0;          
        }
    }

    function get() public view returns (uint) 
    {
       return storedData;
    }

}

 

if… else Statement – Solidity

Here when the conditions of if statements are True then code inside if block is executed, else the code inside else block is executed.

Below is the syntax of the if… else statement in Solidity.

if (conditions)
{
  // Code inside this block is executed when if conditions are True
}
else
{
  // Code inside this block is executed when if conditions are False
}

Example

In the below example, inside the set function, the state variable storedData is multiplied by 0 if its value is less than 50 else it is multiplied by 2.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;


contract DemoContract 
{
    uint storedData;

    function set(uint x) public 
    {
        storedData = x;

         if (storedData < 50)
        {
            storedData = storedData*0;          
        }
        else
        {
            storedData = storedData*2;
        }

    }

    function get() public view returns (uint) 
    {
        return storedData;
    }

}

 

if… else if Statement – Solidity

In this style, multiple if.. else if blocks are evaluated and the one where the conditions are evaluated to True, the code of that block is executed.

Below is the syntax of the if.. else if statement in Solidity.

if (conditions1)
{
  // Code inside this block is executed when if conditions1 are True
}
else if (conditions2)
{
  // Code inside this block is executed when if conditions2 are True
}
else if (conditions3)
{
  // Code inside this block is executed when if conditions3 are True
}
.
.
.
else if (conditionsN)
{
  // Code inside this block is executed when if conditionsN are True
}
else
{
  // Code inside this block is executed when none of the above conditions are True
}

Example

In the below example there are multiple if… else if blocks to evaluate the value of storedData and accordingly its value is multiplied by a multiplier.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;


contract DemoContract 
{
    uint storedData;

    function set(uint x) public 
    {
        storedData = x;

         if (storedData >=0 && storedData < 50)
        {
            storedData = storedData*0;
            
        }
        else if (storedData >= 50 && storedData <=100)
        {
            storedData = storedData*2;
        }
        else if (storedData >= 100 && storedData <=150)
        {
            storedData = storedData*3;
        }
        else
        {
            storedData = storedData*3;
        }

    }


    function get() public view returns (uint) 
    {
        return storedData;
    }

}