for문은 반복하고자 할 때, 시작점과 시점
ex)학생들의 평균을 구하고자 할 때
if문은 상황에 따라 다른 것을 적용하고자 할 때, 조건
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract FOR {
function loop1() public pure returns(uint) {
uint a;
for(uint i=0; i<5; i++) {
a += i; //a = a+i
}
return a;
}
function loop2() public pure returns(uint, uint) {
uint a;
uint i;
for(i=1; i<=6; i++) {
a += i; //a = a+i
}
return (a,i);
}
function loop3() public pure returns(uint) {
uint a;
for(uint i=1; i<=100; i++) {
a += i;
}
return a;
}
function loop4() public pure returns(uint) {
uint a;
for(uint i=1; i<=100; i+=2) {
a += i;
}
return a;
}
function loop5() public pure returns(uint) {
uint a;
for(uint i=1; i<=100; i*=2) {
a += i;
}
return a;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract FOR {
uint[] numbers;
function push(uint a) public {
numbers.push(a);
}
function getArray() public view returns(uint[] memory) {
return numbers;
}
function addTwoAll() public {
for(uint i=0; i<numbers.length; i++) {
numbers[i] +=2;
}
}
function addTwoAll(uint[] memory _numbers) public pure returns(uint[] memory) {
for(uint i=0; i< _numbers.length; i++) {
_numbers[i] +=2;
}
return _numbers;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract IF {
function grade(uint _score) public pure returns(string memory) {
if(_score>=90) {
return "A";
} else if(_score>=80) {
return "B";
} else if(_score >=70) {
return "C";
} else {
return "F";
}
}
function example(uint _number) public pure returns(string memory) {
if(_number%2==0) {
return "A";
} else if(_number%3==0) {
return "B";
} else if(_number%5==0) {
return "C";
} else {
return "ETC";
}
}
function example2(uint _number) public pure returns(string memory) {
if(_number%2==0 && _number >40 || _number <20) { // && || 위치 바꾸는 것 부터 시작하기
return "A";
} else if(_number <=40 && _number >=20 && _number%2==0) {
return "B";
} else if(_number <=40 && _number >=20 && _number%2!=0) {
return "C";
} else {
return "D";
}
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract IF2 {
function Numbers(uint _number) public pure returns(string memory) {
if(_number >=40 && _number <=60 || _number >=100 && _number <=150 || _number %5==0) {
return "A";
} else if(_number%3==0){
return "B";
} else {
return "C";
}
}
}