Solidity If Else – Else If Conditions

In this article, we’ll take a closer look at Solidity if else else if statements with examples. We’ll also discuss some best practices for using these statements and common pitfalls to avoid.

Solidity conditions are used to evaluate a particular statement and execute a specific action based on the outcome of the evaluation.

They are similar to conditional statements in other programming languages like if-else and else-if conditions.

During the process of writing a program, there might come a time when you need to choose one path out of a set of possible paths that you have been given.

In such cases, you will need to make use of conditional statements in your program in order to be able to make the correct decision and take the correct action.

In Solidity, conditions are used to execute specific actions based on the state of a smart contract.

Let’s take a look at solidity if statements.



Solidity if statement

Solidity uses the if statement to make decisions and execute statements based on conditions.

Syntax

if (expression) {
//Block of code that runs if the corresponding condition is met
}

Here is an example of evaluating a Solidity expression.

The statement(s) given are executed if the resulting value is true.

The statement would not be executed if the expression is false.

Conditions are usually based on comparison operators.

The following program shows if a given number is even or odd:

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint element=20;function getString() public view returns (string memory) { if(element %2 ==0){ return "The following number is Even"; } }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

The following programme evaluates if the user is capable of voting by comparing his/her age:

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint age=20;function check_Age() public view returns (string memory) { if(age<=18){ return "Unfortunately !! you are not eligible to cast your vote"; } }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity If Else statement

Solidity if else condition allow statements to be executed in a more controlled manner.

Syntax

if (expression) {
//Statements to be executed if the condition is met
} else {
//If condition is false, the following block of code is executed
}

An evaluation of a Solidity expression is performed here.

Accordingly, the ‘if‘ block executes the given lines of code if the condition is met.

The else block executes the relevant statements if the expression is false.

The following program illustrates the working of if-else statements:

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint number=3;function check_even_odd() public view returns (string memory) { if(number % 2 ==0){ return "The Number is Even"; } else{ return "The Number is Odd"; } } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity If Else Statements

The following program shows the voter_Age_Checking system using if-else statements:

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint age=26;function voter_Age_Checking() public view returns (string memory) { if(age>=18){ return "Congratulations !! You are eligible to cast your vote "; } else{ return "Unfortunately !! You are underAge to cast your vote "; } } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity if-else if Statement

Solidity if…else if statement is an advanced form of the if…else conditions which allows Solidity to make a correct decision out of a set of conditions when it comes to making a decision.

Syntax

if (expression 1) {
//Executes the block of code if the if condition is met
} else if (expression 2) {
//Displays the code if the corresponding condition is matched
} else if (expression 3) {
//Matches the condition in the expression and executes the code if condition is met
} else {
//If none of the condition is matched from the if , else if block the code in the else block is executed
}

This code isn’t special in any way. Basically, it consists of a series of if statements, where each if is part of the previous statement’s else clause.

Depending on the true condition, statement(s) are executed; if no true condition is observed, then else blocks are executed.

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint mrx=26;function Number_Checker() public returns (string memory) { if(mrx % 2 == 0){ return "The Number is Even"; } else if(mrx >20 ){ return "The Given Number is greater than 20 "; } else{ return "The Number is not Even and not greater than 20"; } } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

pragma solidity ^0.8.0; contract SolidityTest{uint age=21;function Age_Checker() public returns (string memory) { if(age>20){ return "Congratulations !! You are Eligible to Cast your vote "; } else if(age>50){ return "Now are allowed to vote from the mobile Application"; } else{ return "The user have an age less than 20"; } } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

Solidity If…else conditions are an essential aspect of programming in the Solidity language for Ethereum smart contracts. They allow developers to create reliable and secure contracts that can handle a range of different situations and ensure the proper execution of the contract code.

By understanding the different types of conditions available, developers can ensure that their smart contracts function as intended, providing a solid foundation for the creation of decentralized applications on the Ethereum network.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *