Solidity Syntax – What is the Use of Question Mark ‘?’ in Solidity

blockchainsolidity

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

Best 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;
}
Related Topic