0

In solidity I came across a code uint256 q = p % 2 != 0 ? a : b; what does it mean ?

0
2

It's a regular inline if statement, found in many modern languages.

It means "set the value q to be either a or b - decided whether p % 2 != 0 gives true or false.

This can be rewritten as:

uint256 q;
if (p % 2 != 0) {
  q = a;
}
else {
  q = b;
}
2
  • 1
    It's called the "ternary operator", "conditional operator" or "inline if", not "if and only if"
    – Jesbus
    Oct 27 '20 at 14:25
  • 1
    Cheers, fixed. If and only if comes from one not-so-modern language long time ago... Oct 27 '20 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.