1

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

0

1 Answer 1

4

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
    Commented Oct 27, 2020 at 14:25
  • 1
    Cheers, fixed. If and only if comes from one not-so-modern language long time ago... Commented Oct 27, 2020 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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