DEV Community

Cover image for Day 9 - Decision Making
Vedant Chainani
Vedant Chainani

Posted on • Edited on

Day 9 - Decision Making

GitHub logo Envoy-VC / 30-Days-of-Solidity

30 Days of Solidity step-by-step guide to learn Smart Contract Development.

This is Day 9 of 30 in Solidity Series
Today I Learned About Decision Making in Solidity.

If Statement

This is the most basic conditional statement. It is used to make a decision whether the statement or block of code will be executed or not. If the condition is true the statements will be executed, else no statement will execute.

Syntax -

if (expression) {
    Statement(s) to be executed if expression is true
}
Enter fullscreen mode Exit fullscreen mode

Example -

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract MyContract {
    uint i = 4;
    function ifStatement() public returns(bool) {
        if(i>5) {
            return true;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output -

"0": "bool: false"
Enter fullscreen mode Exit fullscreen mode

If - Else Statements

This statement is the next form of conditional statement which allows the program to execute in a more controlled way. Here if the condition is true then the, if block is executed while if the condition is false then else block, is executed.

If - Else Statements

Syntax -

if (expression) {
    Statement(s) to be executed if expression is true
} else {
    Statement(s) to be executed if expression is false
}
Enter fullscreen mode Exit fullscreen mode

Example -

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract MyContract {
    uint i = 4;

    function ifElseStatement() public returns (string memory) {
        if (i > 5) {
            return "i is greater than 5";
        } else {
            return "i is less than 5";
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output -

"0": "string: i is less than 5"
Enter fullscreen mode Exit fullscreen mode

If-Else If-Else Statement

This is a modified form of if…else conditional statement which is used to make a decision among several options. The statements start execution from if statement and as the condition of any if block is true the block of code associated with it is executed and rest if are skipped, and if none of the condition is true then else block executes.

Syntax -

if (expression 1) {
    Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
    Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
    Statement(s) to be executed if expression 3 is true
} else {
    Statement(s) to be executed if no expression is true
}
Enter fullscreen mode Exit fullscreen mode

Example -

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract MyContract {
    uint i = 4;

    function ifElseIfElseStatement() public returns (string memory) {
        if (i < 2) {
            return "i is less than 2";
        } else if (i >= 2 && i < 8) {
            return "i is between 2 and 8";
        } else {
            return "i is greater than 8";
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Output -

"0": "string: i is between 2 and 8"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)