What is decision making in Solidity?
Overview
Decision-making statements or conditional statements control the code execution based on certain conditions.
Solidity supports the following decision-making statements that can be used while creating contracts.
The if statement
The if statement allows the execution of a block of code if the given condition is true. Otherwise, it will skip that block of code.
Syntax
The syntax of the if statement is given below:
if (condition) {
// block of code
}
Example
In the following code snippet, we will see how to use the if statement in Solidity:
pragma solidity ^0.5.0;contract Example {// declaring state variablesuint i = 10;uint j = 15;// creating a function to demonstrate if statementfunction ifStatementExample() public view returns(string memory){// creating an if statementif (i < j) {return "i is smaller than j";}}}
Explanation
- Line 3: We create a contract named
Example. Acontractis a code block that contains functions and state variables. It is stored on a specific address on the Ethereum blockchain. - Line 5-6: We declare two
uintstate variablesiandj.uintis a solidity data type that represents an unsigned integer. - Line 9: We create a function
ifStatementExample. - Line 11-13: We create an
ifstatement that checks the conditioni < jand executes the code based on the result. If the condition is true, the if statement block will be executed and the function will returni is smaller than jas output.
The if else statement
The if else statement executes a code block based on a given condition.
If the condition is true, it will execute the if block. If the condition is false, it will execute the else block.
Syntax
The syntax of the if else statement is given below:
if (condition) {
// code
} else {
// code
}
Example
In the following code snippet, we will see how to use the if else statement in Solidity:
pragma solidity ^0.5.0;contract Example {// declaring state variablesuint i = 10;uint j = 5;// creating a function to demonstrate if-else statementfunction ifElseStatementExample() public view returns(string memory){// creating an if-else statementif (i < j) {return "i is smaller than j";} else {return "i is greater than or equal to j";}}}
Explanation
- Line 3: We create a contract named
Example. - Line 5-6: We declare two
uintstate variablesiandj. - Line 9: We create a function
ifElseStatementExample. - Line 11-15: We create an
if-elsestatement that checks the conditioni < j. It then executes the code based on the result. If the condition is true, theifstatement block will be executed and the function will returni is smaller than jas output. If the condition is false, theelsestatement block will be executed and the function will returni is greater than or equal to jas output.
The if else if else statement
The if else if else statement is a series of if statements where each if is attached with the else of the previous statement.
If one of the conditions is true, that statement’s block of code will be executed. If none of the of the conditions are true, the else block will be executed.
Syntax
if (condition1) {
// code
} else if (condition2) {
// code
} else if (condition3) {
// code
} else {
// code
}
Example
In the following code snippet, we will use the if else if else statement in Solidity:
pragma solidity ^0.5.0;contract Example {// declaring state variablesuint i = 10;uint j = 5;// creating a function to demonstrate if-else-if statementfunction ifElseIfStatementExample() public view returns(string memory){// creating an if-else-if statementif (i < j) {return "i is smaller than j";} else if (i > j) {return "i is greater than j";} else {return "i is equal to j";}}}
Explanation
- Line 3: We create a contract named
Example. - Line 5-6: We declare two
uintstate variablesiandj. - Line 9: We create a
ifElseIfStatementExamplefunction. - Line 11-17: We create an
if-else-ifstatement that checks two conditionsi < jandi > j. It then executes the code based on the result. If the conditioni < jis true, theifstatement block will be executed and the function will returni is smaller than jas an output. If the conditioni > jis true, theelse ifstatement block will be executed and the function will returni is greater than jas an output. If none of the above conditions are true, theelsestatement block will be executed and the function will returni is equal to jas an output.