Solidity Comparision Operator

Detailed Examples of Comparison Operators in Solidity

1. == (Equal to):

Solidity

uint a = 10;
uint b = 20;

if (a == b) {
    // This block will not be executed
} else {
    // This block will be executed
}

In this example, a is not equal to b, so the else block is executed.

2. != (Not equal to):

Solidity

address owner = 0x1234567890abcdef;
address sender = 0xabcdef1234567890;

if (owner != sender) {
    // This block will be executed
} else {
    // This block will not be executed
}

In this example, owner is not equal to sender, so the if block is executed.

3. < (Less than):

Solidity

uint age = 25;
uint votingAge = 18;

if (age < votingAge) {
    // This block will not be executed
} else {
    // This block will be executed
}

In this example, age is not less than votingAge, so the else block is executed.

4. > (Greater than):

Solidity

uint balance = 100;
uint requiredBalance = 50;

if (balance > requiredBalance) {
    // This block will be executed
} else {
    // This block will not be executed
}

In this example, balance is greater than requiredBalance, so the if block is executed.

5. <= (Less than or equal to):

Solidity

uint quantity = 10;
uint maxQuantity = 10;

if (quantity <= maxQuantity) {
    // This block will be executed
} else {
    // This block will not be executed
}

In this example, quantity is less than or equal to maxQuantity, so the if block is executed.

6. >= (Greater than or equal to):

Solidity

uint score = 85;
uint passingScore = 70;

if (score >= passingScore) {
    // This block will be executed
} else {
    // This block will not be executed
}

In this example, score is greater than or equal to passingScore, so the if block is executed.

These examples demonstrate how comparison operators can be used to make decisions and control the flow of execution in your Solidity smart contracts.

Leave a Reply