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 allows the execution of a block of code if the given condition is true. Otherwise, it will skip that block of code.
The syntax of the if statement is given below:
if (condition) {
// block of code
}
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";}}}
Example. A contract is a code block that contains functions and state variables. It is stored on a specific address on the Ethereum blockchain.uint state variables i and j. uint
is a solidity data type that represents an unsigned integer.ifStatementExample.if statement that checks the condition i < j and executes the code based on the result.
If the condition is true, the if statement block will be executed and the function will return i is smaller than j as output.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.
The syntax of the if else statement is given below:
if (condition) {
// code
} else {
// code
}
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";}}}
Example.uint state variables i and j.ifElseStatementExample.if-else statement that checks the condition i < j. It then executes the code based on the result. If the condition is true, the if statement block will be executed and the function will return i is smaller than j as output. If the condition is false, the else statement block will be executed and the function will return i is greater than or equal to j as output.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.
if (condition1) {
// code
} else if (condition2) {
// code
} else if (condition3) {
// code
} else {
// code
}
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";}}}
Example.uint state variables i and j.ifElseIfStatementExample function.if-else-if statement that checks two conditions i < j and i > j. It then executes the code based on the result.
If the condition i < j is true, the if statement block will be executed and the function will return i is smaller than j as an output. If the condition i > j is true, the else if statement block will be executed and the function will return i is greater than j as an output. If none of the above conditions are true, the else statement block will be executed and the function will return i is equal to j as an output.