How to write an if statement in Solidity

An if statement in Solidity is a conditional statement that, if proved true, performs a function. The statement is executed based on a condition being true. If none of the conditions are true, then the else block is executed.

Below is an definition of the if statement expression and a few simple examples. These examples are to help you identify the if statement patterns so you can rewrite them in your smart contracts.

The basic syntax of a solidity if statement is as follows:

``````if (evaluate expression #1) {
//execute a statement if expression #1 above is true

} else if (evaluate expression #2) {
//execute a statement if expression #2 above is true

} else {
//Statement to be executed if no expression is true

}``````

Try it in Remix

As a reminder below is a sample smart contract that sets a state variable. We will use this as a basis to build our if statement.

``````//Sample contract is an example to save x to a state variable
contract SampleContract {

uint public x;

function numbers(uint _x) public {
x = _x;

}
}

//or

contract SampleContract {

uint public x;

function numbers(uint _x) public returns(uint){
x = _x;
return _x;

}
}``````

Try it in Remix

Now lets take the sample contract a step further and add an if condition to the function.

``````//Sample contract with an if statement which saves results to a state variable

pragma solidity ^0.8.1;

contract sampleContract {

uint public x;

function numbers(uint _x) public {
if (_x < 25 ) {
x = 0;
} else if (_x < 50 ) {
x = 1;
}
else {
x = 2;
}
}
}

//or

contract SampleContract {

uint public x;

function numbers(uint _x) public returns(uint){
if (_x < 25 ) {
x = 0;
return x;
} else if (_x < 50 ) {
x = 1;
return x;
}
else {
x = 2;
return x;
}
}
}``````

Try it in Remix

Now lets create a function that takes a uint, evaluates the input in an if condition, and save a string to a state variable.

``````pragma solidity ^0.8.1;

contract SymbolReturn {

string public symbol;

//input 1 or 2 in the function and save BTC or ETH to the state variable
function symbolNum(uint _symbolNum) public {

if (_symbolNum == 1 ) {
symbol = 'BTC';

} else if (_symbolNum == 2 ) {
symbol = 'ETH';

} else {
symbol = 'CRAPCOIN';
}

}

}``````

Try it in Remix

This code is for learning and entertainment purposes only. Use this code at your own risk. Remember smart contracts are experimental and could contain bugs.

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Nodes

Learn how to run a Geth node. Read getting started with Geth to run an Ethereum node.

Fix a transaction

How to fix a pending transaction stuck on Ethereum or EVM compatible chain