Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • If Else Keyword (Condition)
  • Short If
  1. STEP 2

12: Condition

If Else Keyword (Condition)

The if else keyword is an important part of Solidity and is used to control the execution of code based on the evaluation of a condition. The keyword is usually used to check for logical conditions, such as checking if a variable is equal to a certain value.

The syntax for an if else statement in Solidity is as follows:

if (condition) {    
    // execute this code if condition is true
} else {    
    // execute this code if condition is false
}

For example, consider the following code snippet:

uint age = 21;
if (age < 18) {    
    // execute this code if age is less than 18
} else {   
     // execute this code if age is greater than or equal to 18
}

In this example, the code within the if statement will be executed if the age variable is less than 18, and the code within the else statement will be executed if the age variable is greater than or equal to 18.


Short If

Short if is a shorthand for if-else statements. It is a ternary operator that can be used to shorten an "if-else" statement.

Syntax:

condition ? expression1 : expression2;

A condition is evaluated to either true or false. If the condition evaluates to true, the expression1 is executed, otherwise expression2 is executed.

Example:

int x = 17;
string memory result = (x > 0) ? "positive" : "non-positive";

The above statement evaluates the condition x > 0 and assigns the value "positive" to the result if the condition is true, otherwise assigns the value "non-positive" to the result. Example:

int x = 17;
bool res = (x > 0) ? true : false;

That's it for the lesson 12! In the next lesson, While Loop

Previous11: EventsNext13: While Loop

Last updated 1 year ago