Skip to main content

Command Palette

Search for a command to run...

Conditional Operator

Published
•1 min read•View as Markdown
Conditional Operator

Conditional Operator

  1. In Solidity, the conditional operator is written using the "?" and ":" symbols and is also known as the ternary operator. The syntax for the conditional operator is as follows:

     (condition) ? (value if true) : (value if false);
    

    The conditional operator is often used as a shorthand for simple if-else statements. Here's an example:

    
     function getLargerNumber(uint a, uint b) public view returns (uint) {
       return (a > b) ? a : b;
     }
    

    In the example above, the function takes two unsigned integers, a and b, and returns the larger of the two using the conditional operator. If a is greater than b, then the expression (a > b) evaluates to true, and the value of a is returned. Otherwise, the expression evaluates to false, and the value of b is returned.

More from this blog