2020.코딩일지
[solidity] if문/반복문/continue와break/equal (feat.솔리디티깨부수기) 본문
728x90
if문
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract lec17 {
string private outcome = "";
function inIt5(uint256 _number) public returns(string memory) {
if(_number ==5){
outcome = "Yes, it is 5";
return outcome;
}
else {
outcome = "No, it is not 5";
return outcome;
}
}
function inIt5or3or1(uint256 _number) public returns(string memory){
if(_number == 5) {
outcome = "Yes, it is 5";
return outcome;
}
else if(_number == 3) {
outcome = "Yes, it is 3";
return outcome;
}
else if(_number == 1) {
outcome = "Yes, it is 1";
return outcome;
}
else {
outcome = "No, it is not 5, 3 or 1";
return outcome;
}
}
}
반복문
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//for, while, do-while
contract lec18 {
event CountryIndexName (uint256 indexed _index, string _name);
string[] private countryList = ["South Korea", "North Korea", "USA","China","Japan"];
//for문
function forLoopEvents() public {
for(uint256 i = 0; i<countryList.length; i++) {
emit CountryIndexName(i, countryList[i]);
}
}
//while문
function whileLoopEvents() public {
uint256 i = 0;
while(i<countryList.length) {
emit CountryIndexName(i, countryList[i]);
i++;
}
}
//do-while문
function doWhileLoopEvents() public {
uint256 i = 0 ;
do {
emit CountryIndexName(i, countryList[i]);
i++;
}
while(i<countryList.length);
}
}
continue와 break
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract lec19 {
//continue : 다음 반복문으로 이동
//break: 반복문을 끝냄
event CountryIndexName(uint256 indexed _index, string _name);
string[] private countryList = ["South Korea", "North Korea", "USA", "China", "Japan"];
function useContinue() public {
for(uint256 i=0; i<countryList.length; i++){
if( i%2 == 1 ){ //홀수인경우
continue; //i++
}
emit CountryIndexName(i, countryList[i]); //짝수만출력
}
}
function useBreak() public {
for(uint256 i=0; i<countryList.length; i++) {
if(i==2){ //인덱스0,1만출력됨
break; //2에서종료
}
emit CountryIndexName(i, countryList[i]);
}
}
}
equal
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract lec20 {
event CountryIndexName(uint256 indexed _index, string name);
string[] private countryList = ["South korea", "North Korea", "USA", "China", "Japan"];
function linearSearch(string memory _search) public view returns(uint256, string memory){
for(uint256 i=0; i<countryList.length; i++){
//솔리디티에는 비교하는 기능이 없다. "aaa"==="aaa", "aa".isEqual("Aa")
//bytes화해서 keccak256돌려서 같은지 비교하는 방법만 있다!
if(keccak256(bytes(countryList[i])) == keccak256(bytes(_search))){
return (i, countryList[i]);
}
}
return(0, "Nothing"); //for문다돌았는데도 없으면 출력...대소문자구별하네???헐...ㅋ
}
}
'Block Chain' 카테고리의 다른 글
[solidity] return/SPDX (feat.솔리디티깨부수기) (0) | 2022.12.05 |
---|---|
[solidity] error/try-catch문 (feat.솔리디티깨부수기) (0) | 2022.12.05 |
[solidity] struct구조체 (feat.솔리디티깨부수기) (0) | 2022.11.30 |
[solidity] mapping/array (feat.솔리디티깨부수기) (11) | 2022.11.29 |
[solidity] event/indexed/super (feat.솔리디티깨부수기) (0) | 2022.11.29 |
Comments