本篇預計閱讀時間 20 分鐘, 實作時間 30 分鐘。
在開始之前先來思考底下的練習題,閱讀後即可練習完成。
練習題
-
練習題 1
-
如何寫出一個可以跑十次迴圈的函數,每次將一輸入的整數 + 1,計算
uint a = 10
經過迴圈的計算結果。
-
-
練習題 2
-
如何寫出一個可以判斷奇數的函數?
-
-
練習題 3
-
小明賣水果,已知水果一斤 3 ether,如何寫出一個函式
sell()
每次執行可以賣出一斤水果以及一個函式profit()
計算當前賺了多少 wei?
-
-
練習題 4
-
如何寫出一個函數可以讀取 Operation:add、sub,並且回傳 1+1 分別經過 +、- 兩種運算的結果。
-
If-Else
判斷語法(Conditional Operators)和其他的程式語言的用法一樣,同樣支援 else if 、三元運算子等判斷語法。
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract IFELSE {
function isEvenNumber(uint _number) public view returns(bool){
if(_number % 2 == 0) {
return true;}
else {
return false;
}
}
// 一樣可以使用 else if 來做更多元的其他判斷
if (expression 1) {
// 執行, 若 expression 1 為 true
// Statement(s) to be executed if expression 1 is true//
} else if (expression 2) {
// 執行, 若 expression 2 為 true
// Statement(s) to be executed if expression 2 is true//
} else if (expression 3) {
// 執行, 若 expression 3 為 true
// Statement(s) to be executed if expression 3 is true//
} else {
// 執行, 若以上 expression 皆不為 true
// Statement(s) to be executed if no expression is true
}
// 三元運算子
function isEvenNumber_tri(uint _number) public view returns(bool) {return (_number % 2 == 0 ? true : false);
}
}
For
迴圈語法也和其他程式語言相差無幾,主要有初始敘述、條件敘述、每圈結束的敘述組成,使用上和 C/C++ 一樣。
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract MyContract {uint[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function countEvenNumbers() public view returns (uint) {
uint count = 0;
for(uint i = 0; i < numbers.length; i++){
if(isEvenNumber(numbers[i])) {
count ++;
}
}
return count;
}
function isEvenNumber(uint _number) public view returns(bool) {return (_number % 2 == 0 ? true : false);
}
}
While
迴圈語法也和其他程式語言一模一樣,由 while 後方以小括弧包住 boolean statement, 以下這個例子跟上面 for 迴圈的例子互為相對。
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract MyContract {uint[] public numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function countEvenNumbers() public view returns (uint) {
uint count = 0;
uint i = 0;
while(i < numbers.length){
if(isEvenNumber(numbers[i])) {
count ++;}
i ++;
}
return count;
}
function isEvenNumber(uint _number) public view returns(bool) {
return (_number % 2 == 0 ? true : false);
}
}
練習題解答
-
練習題 1
-
寫出一個可以跑十次迴圈的函數,每次將一輸入的整數 + 1,計算
uint a = 10
經過迴圈的計算結果。
-
//SPDX-License-Identifier: MIT pragma solidity ^0.8.11; contract Problem {
uint public a = 10; function ForLoop() public {
for(uint i = 0; i < 10; i++){ a++;
} }
}
-
練習題 2
-
寫出一個可以判斷奇數的函數
-
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract Problem {
function isOddNumber(uint _number) public view returns(bool) { return (_number % 2 != 0 ? true : false);
}
}
-
練習題 3
-
小明最近在賣水果,已知水果一斤 3 ether,請寫出一個函式
sell()
每次執行可以賣出一斤水果,以及一個函式profit()
計算當前賺了多少 wei。
-
pragma solidity ^0.8.11;
contract Problem { uint amount = 0; uint public Profit = 0; function sell() public {
amount++; }
function profit() public { Profit = amount * 3000 * (10 ** 18);
}
}
-
練習題 4
-
請寫出一個函數可以讀取 Operation:add、sub,並且回傳 1+1 分別經過 +、- 兩種運算的結果。
-
pragma solidity ^0.8.11;
contract String { int public answer = 10; function Operator(string memory operation) public{
if (keccak256(abi.encodePacked(operation)) == keccak256(abi. encodePacked("add"))){
answer = 1 + 1;
}
if (keccak256(abi.encodePacked(operation)) == keccak256(abi. encodePacked("sub"))){
answer = 1 - 1;
}
}
}