Conditionals, also known as “if/else statements”, in Solidity are very simple to write and allow a developer to create a contract that behaves in different ways based on the current state of the blockchain. Here is a simple if/else smart contract:
//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract ifElse{
function isZero(uint _input) public view returns (bool){
if(_input == 0){
//do something if the value is zero
return true;
}else{
//do something if the value is not zero
return false;
}
}
function isEvenOrZero(uint _input) public view returns(bool){
if(_input == 0){
return true;
}else if(_input %2 == 0){
return false;
}else{
return true;
}
}
function isZeroTernary(uint _input) public view returns (bool){
return _input == 0 ? true : false;
}
}
In this contract, we have 3 functions that accept an unsigned number, and return a boolean.
In function 1 named isZero, we use an if/else statement to return true if the input is 0. If the inputted value is not zero, you can see the function will return false.
In function 2, named IsZeroOrEven, we write a more complex function that uses an if, ifelse, and else statement. In the first case, if the input is equal to 0, we return true. In the second case, which is only computed if the first case is false, we then see if the number is odd, and if it is, we return false. This leave another case, if the number is not 0 and not odd, we can write an else statement that returns true if it passes both of these conditionals.
In function 3, we essentially write the same function as function 1, except in this case we use a ternary so that there is less total code.
Operators
There are many things you can check inside the parenthesis of a conditionals, including comparing two variables, checking a boolean, or even checking many variables. Here are some operators you can check:
< Less than
> Greater than
== Equal to
<= Less than OR equal to
>= Greater than OR equal to
!= Not equal to
Let’s say you want to ensure that there are two variables which are true inside a conditional, here is how you would do it:
if(one && two)
if(a<b && b<c)
Of course, these variables could be replaced with conditionals themselves, so you can check many things this way.
There’s also the case when you want to check that one of two things are true. Here is how you can check that at least one variable or condition is true:
if(one || two)
if(a<b || a<c)
Solidity Ternary
A ternary is a single line of code that is a shortened way to write an if else conditional statement. This is how you write a ternary:
//a return statement
return _input == 0 ? true : false
//a computation statement
x >= 5 ? x++ : x--;
When you return something using a ternary, the return statement comes first, then the conditional to check, and finally the two possible outcomes separated with a colon.
When you just want to use a ternary to perform some computation, you start it with the condition to check. In the second example, we are checking that x is less than or equal to the value of 5. Then we must use a question mark to indicate that this is a ternary. After that, we write some code for the smart contract to perform if the condition is true, which is “x++” in our case. After the true outcome, we separate it with a colon and write our false outcome. In this case, we have used “x–“.
If you’re wondering what a question mark is used for in Solidity, the answer is that it is to designate a ternary operator.