当前位置:网站首页>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);
}边栏推荐
- 连接MySQL时报错:Public Key Retrieval is not allowed 【解决方法】
- Shell condition test, judgment statement and operator of shell system learning
- Expose Prometheus metrics in Perl programs
- UUID and secrets module
- flink去重(一)flink、flink-sql中常见的去重方案总结
- IDEA中文乱码怎么办
- IO中节点流和处理流的理解学习
- 小程序消息推送配置 Token校验失败,请检查确认
- Compiling and using log4cxx in rhel7.3
- Flink principle (I) TTL management and fault tolerance mechanism of state
猜你喜欢

Apifox安装及使用

记录一个自己挖的坑~

flink1.14 sql基础语法(一) flink sql表查询详解

什么是真正的 HTAP ?(二)挑战篇

C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)

C语言实现猜数字小游戏项目实战(基于srand函数、rand函数,Switch语句、while循环、if条件判据等)

(2022 Niuke multi school III) a-ancestor (LCA)

Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query

Introduction to network -- overview of VLAN and trunk

(2022 Hangdian multi school III) 1009.package delivery (greedy)
随机推荐
闭散列和开散列解决哈希冲突
User unlock sm04 sm12
Am I delayed by the code... Unfortunately, I became a programmer
Okaleido ecological core equity Oka, all in fusion mining mode
Regular expression foundation sorting
Top ten interview questions for software testing (with answers and analysis)
shell循环练习
Turn off the auto start function of Oracle service in centos7
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
mysql备份策略
Analysis of query results using both left join on and where in MySQL
flink去重(二)解决flink、flink-sql去重过程中的热点问题
Pg_ relation_ Size question
[QT] unable to open the containing file pcap.h (C1083) in QT creator
TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)
The DrawImage method calls the solution of not displaying pictures for the first time
Single arm routing (explanation + experiment)
MySQL backup strategy
Demonstrate the use of foreign keys with Oracle
Help send a recruitment, base all over the country. If you are interested, you can come and have a look