envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
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
}
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;
}
}
}
Output -
"0": "bool: false"
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.
Syntax -
if (expression) {
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
}
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";
}
}
}
Output -
"0": "string: i is less than 5"
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
}
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";
}
}
}
Output -
"0": "string: i is between 2 and 8"

Top comments (0)