Skip to content

Instantly share code, notes, and snippets.

@Ifeanyi-Ani
Created August 14, 2023 12:22
Show Gist options
  • Save Ifeanyi-Ani/58802c9faf89f7fb3b2437055ce48c3c to your computer and use it in GitHub Desktop.
Save Ifeanyi-Ani/58802c9faf89f7fb3b2437055ce48c3c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.21+commit.d9974bed.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract IfElse {
function foo(uint x) public pure returns (uint) {
if (x < 10) {
return 0;
} else if (x < 20) {
return 1;
} else {
return 2;
}
}
function ternary(uint _x) public pure returns (uint) {
if (_x < 10) {
return 1;
}
return 2;
// shorthand way to write if / else statement
// the "?" operator is called the ternary operator
// return _x < 10 ? 1 : 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment