If Else statement in Solidity

If Else statement in Solidity

Solidity's if-else provides conditional code execution based on a Boolean condition.

If statement

When writing a program, there may be a situation in which you must choose one of a number of possible options from a given set of options. It is necessary to utilize conditional statements in such situations to ensure that your program makes the correct decisions and performs the appropriate actions.

Conditional statements are supported in Solidity, and they can be used to conduct multiple actions depending on distinct conditions. In this section, we will go through the if statement.

pragma solidity ^0.8.0;

contract IfStatement{

    function globalVariables(uint a) public returns(string memory){
        if (a<10){
            return "Welcome to My Blog";
        }
    }
}

If Else statement

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

contract IfElse {
    function person(uint x) public returns (string memory) {
        if (x < 10) {
            return "person A";
        } else if (x < 20) {
            return "person B";
        } else {
            return "person C";
        }
    }
}

Nested If Else Statement

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

contract IfElse {
    function person(uint x) public returns (string memory) {
        if (x < 10) {
            return "person A";
        } else if (x < 20) {
            if (x<15 && x>10){
                return "person B";
            }
            else if (x>15 && x<20){
                return "person C";
            }

        } else {
            return "person D";
        }
    }
}

For more content, follow me on - https://linktr.ee/shlokkumar2303

Did you find this article valuable?

Support Shlok Kumar by becoming a sponsor. Any amount is appreciated!