Solidity支持条件语句,让程序可以根据条件执行不同的操作。条件语句包括:

  • if
  • if...else
  • if...else if

语法

if (条件表达式) {
被执行语句(如果条件为真)
}

示例

展示if语句用法。

pragma solidity ^0.5.0;

contract SolidityTest {
uint storedData;
constructor() public {
storedData = 10;
}
function getResult() public view returns(string memory){
uint a = 1;
uint b = 2;
uint result = a + b;
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) { // if 语句
return "";
}
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 – if…else语句

语法

if (条件表达式) {
被执行语句(如果条件为真)
} else {
被执行语句(如果条件为假)
}

示例

展示if...else语句用法。

pragma solidity ^0.5.0;

contract SolidityTest {
uint storedData;
constructor() public{
storedData = 10;
}
function getResult() public view returns(string memory){
uint a = 1;
uint b = 2;
uint result
if( a > b) { // if else 语句
result = a;
} else {
result = b;
}
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) {
return "";
}
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: 2

Solidity – if…else if…语句

语法

if (条件表达式 1) {
被执行语句(如果条件 1 为真)
} else if (条件表达式 2) {
被执行语句(如果条件 2 为真)
} else if (条件表达式 3) {
被执行语句(如果条件 3 为真)
} else {
被执行语句(如果所有条件为假)
}

示例

展示if...else if...语句用法。

pragma solidity ^0.5.0;

contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view 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 "";
}
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

参考区块链入门到实战(31)之Solidity – 第一个程序运行

最新文章

  1. js取两个数组的交集|差集|并集|补集|去重示例代码
  2. EF增删改查操作
  3. EF(Entity Framework)发生错误”正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不保证 DbContext 的实例成员和相关类是线程安全的。 临时解决了这个问题,在Context的构造函数中,禁用了自动初始化:
  4. elasticsearch api
  5. Tomcat启动时自动加载一个类
  6. rac 10g 加入节点具体解释
  7. android狼人杀源码,桌面源码,猎豹快切源码
  8. 解决Visual Studio 2017隐藏“高级保存选项”命令
  9. 简单的C#网络爬虫
  10. 机器A定时发文件给机器B-FTP实现
  11. Liunx中fstab文件详解
  12. 201806 数据处理 SQL、python、shell 哪家强...速度PK(上篇)
  13. 【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件
  14. powershell脚本执行绕过powershell下脚本执行限制(cmd下执行)以及在cmd下隐藏脚本窗口
  15. python collection模块
  16. Map 嵌套存储Map
  17. elasticsearch搜索引擎环境的搭建
  18. Nvme固体硬盘Intel750,SM961分别使用一段时间以后对比
  19. gevent 实现单线程下的socket链接
  20. SQL简单基础(1)

热门文章

  1. Python time tzset()方法
  2. PHP mysqli_thread_id() 函数
  3. 数据结构C语言实现----选择排序
  4. linux的存储管理(RALD) LVM 逻辑卷管理 虚拟阵列
  5. win10 64位 汇编环境
  6. 用 Python 写个消消乐小游戏
  7. react 样式冲突解决方案 styled-components
  8. 什么才是定制化IDE的核心价值?
  9. C#LeetCode刷题之#859-亲密字符串​​​​​​​​​​​​​​(Buddy Strings)
  10. JavaScript Array.map + parseInt