Skip to content

Instantly share code, notes, and snippets.

@raj-pranav
Last active February 3, 2022 20:07
Show Gist options
  • Save raj-pranav/440ec3f10d1e7f11fb889757e7640106 to your computer and use it in GitHub Desktop.
Save raj-pranav/440ec3f10d1e7f11fb889757e7640106 to your computer and use it in GitHub Desktop.
control structure - if else
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract demo_if {
uint x = 10;
function Compare (uint _x) external view returns (string memory){
if (_x >= x) { // if section
return 'Given value is greater or equal to x';}
else { // else section
return 'Given value is less than x';
}
}
}
if (condition) {
body ;}
else if (another condition) {
body ; }
else {
body ; }
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract demo_if {
uint x;
function CompNum (uint _x) public view returns (string memory) {
if (_x > x) {
return 'The number entered is bigger';
} else if (_x < x) {
return 'Number entered is lesser';
} else {
return 'Both numbers are equal';
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract demo_if2 {
function CompNum (uint _x) public pure returns (string memory) {
if (_x == 1) {
return 'Monday'; }
else if (_x == 2) {
return 'Tuesday';}
else if (_x == 3) {
return 'WednesDay';}
else if (_x == 4) {
return 'Thursay';}
else if (_x == 5) {
return 'Friday';}
else if ((_x == 6) || (_x == 7)) {
return 'It is a Weekend';}
else {
return 'Invalid Input';
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract demo_if {
uint x = 10;
function Compare (uint _x) external view returns (bool){
if (_x >= x) { // only one if statement
return true;
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract demo_if {
uint x = 10;
function Compare (uint _x) external view returns (string memory){
return _x >= x ? 'Given value is greater or equal to x' : 'Given value is less than x' ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment