I wanted to ask if is it available to use OR operator in IF conditions, because its giving me awkward results, like this:
contract test {
if(player1Points > Player2Points || player1Points >= 50){
result = 1;
}
}
any help ??
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI wanted to ask if is it available to use OR operator in IF conditions, because its giving me awkward results, like this:
contract test {
if(player1Points > Player2Points || player1Points >= 50){
result = 1;
}
}
any help ??
Yes, you can use it.
From the docs:
The operators
||
and&&
apply the common short-circuiting rules. This means that in the expressionf(x) || g(y)
, iff(x)
evaluates totrue
,g(y)
will not be evaluated even if it may have side-effects.
Try with this code:
pragma solidity ^0.5.0;
contract Test {
function comparePoints (uint player1Points, uint player2Points) public pure returns (uint) {
uint result;
if(player1Points > player2Points || player1Points >= 50){
result = 1;
}
return result;
}
}
Maybe the following sections will be useful to start writing smart contracts.