Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 146.5MB ·虚拟内存 1437.8MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
if (条件表达式 1) {
被执行语句(如果条件 1 为真)
} else if (条件表达式 2) {
被执行语句(如果条件 2 为真)
} else if (条件表达式 3) {
被执行语句(如果条件 3 为真)
} else {
被执行语句(如果所有条件为假)
}
展示 if...else if... 语句用法:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SolidityTest {
uint storedData; // State variable
constructor() {
storedData = 10;
}
function getResult() public pure returns(string memory) {
uint a = 1;
uint b = 2;
uint c = 3;
uint result
if( a > b && a > c) { // if else if 语句
result = a;
} else if( b > a && b > c ){
result = b;
} else {
result = c;
}
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);// 访问局部变量
}
}
运行上述程序,输出结果:
0: string: 3
与其他语言类似,Solidity语言支持循环结构,Solidity提供以下循环语句。whiledo ... whilefor循环控制语句:break、continue。Solidity 循环语句包括如下形式:So ...