当前位置:网站首页>Solidity智能合约开发 — 3.3-solidity语法控制结构
Solidity智能合约开发 — 3.3-solidity语法控制结构
2022-07-27 07:05:00 【Crypto168】
solidity的控制结构有if, else, while, do, for, break, continue, return。这些关键字在C语言和JavaScript中表达式相同, 学好这三板斧所有的逻辑结构都好实现。
1. if-else
function test(uint256 _number) public pure returns(bool){
if(_number == 0){
return(true);
}else{
return(false);
}
}2. for循环
function test() public pure returns(uint256){
uint sum = 0;
for(uint i = 0; i < 100; i++){
sum += i;
if(sum > 50){
break;
}
}
return(sum);
}3. do-while循环
function test() public pure returns(uint256){
uint sum = 0;
uint i = 0;
do{
if(sum < 10){
continue;
}
sum += i;
i++;
}while(i < 100);
return(sum);
}4. 用solidity实现插入排序
// 插入排序
function insertionSort(uint[] memory a) public pure returns(uint[] memory) {
// note that uint can not take negative value
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
uint j=i;
while( (j >= 1) && (temp < a[j-1])){
a[j] = a[j-1];
j--;
}
a[j] = temp;
}
return(a);
}边栏推荐
- 2022-07-25 Gu Yujia's study notes
- 电子量产项目框架--基本思想
- 网络入门——vlan及trunk概述
- RestTemplate 连接池配置
- C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)
- Haikang H9 camera cannot be connected with xshell (SSH is not enabled)
- VLAN trunk experiment
- Graylog 日志服务器单节点部署
- Use Amazon dynamodb and Amazon S3 combined with gzip compression to maximize the storage of player data
- shell企业面试题练习
猜你喜欢

VLAN trunk experiment

Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation

Graylog 日志服务器单节点部署

MySQL backup strategy

Panabit SNMP configuration

单片机多级菜单

我是不是被代码给耽误了……不幸沦为一名程序员……

SQLite common function integration

(2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)
![Error when connecting to MySQL: public key retrieval is not allowed [solution]](/img/b3/41523d44924ec203e40453bace6627.png)
Error when connecting to MySQL: public key retrieval is not allowed [solution]
随机推荐
shell企业面试题练习
Properties类和properties配置文件的理解学习
The solution of using sqlplus to display Chinese as garbled code
Shell condition test, judgment statement and operator of shell system learning
(2022 Hangdian multi school III) 1009.package delivery (greedy)
IO中节点流和处理流的理解学习
C common function integration-3
(2022 Niuke multi school III) a-ancestor (LCA)
flink去重(一)flink、flink-sql中常见的去重方案总结
Gossip: Recently, many friends talk about going abroad
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Drconv pytorch is changed to the same size of output and input
Essay: college entrance examination
drawImage方法第一次调用不显示图片的解决方式
Chapter 6 Shell Logic and Arithmetic
STM32_找到导致进入HardFault_Handler的函数
[golang learning notes 2.1] sorting and searching in arrays in golang
Functools module
Oracle composite query
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)