In solidity I came across a code uint256 q = p % 2 != 0 ? a : b;
what does it mean ?
1 Answer
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;
}
-
1It's called the "ternary operator", "conditional operator" or "inline if", not "if and only if"– JesbusCommented Oct 27, 2020 at 14:25
-
1Cheers, fixed. If and only if comes from one not-so-modern language long time ago... Commented Oct 27, 2020 at 17:02