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 variables
uint i = 10;
uint j = 15;
// creating a function to demonstrate if statement
function ifStatementExample() public view returns(string memory){
// creating an if statement
if (i < j) {
return "i is smaller than j";
}
}
}

Explanation

  • Line 3: We create a contract named Example. A contract is 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 uint state variables i and j. uint is a solidity data type that represents an unsigned integer.
  • Line 9: We create a function ifStatementExample.
  • Line 11-13: We create an 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

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 variables
uint i = 10;
uint j = 5;
// creating a function to demonstrate if-else statement
function ifElseStatementExample() public view returns(string memory){
// creating an if-else statement
if (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 uint state variables i and j.
  • Line 9: We create a function ifElseStatementExample.
  • Line 11-15: We create an 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

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 variables
uint i = 10;
uint j = 5;
// creating a function to demonstrate if-else-if statement
function ifElseIfStatementExample() public view returns(string memory){
// creating an if-else-if statement
if (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 uint state variables i and j.
  • Line 9: We create a ifElseIfStatementExample function.
  • Line 11-17: We create an 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.

Free Resources