文章目录

  • 1 条件语句
  • 2.循环语句
  • 2.1 while循环
  • 2.2 for循环
  • 3.break、continue return、


1 条件语句

if/else与java语言一样,如果if/else的子句只有一个语句,“{ }”可以省略
注意:,if(1){······}在Solidity中是无效的,不过可以使用强制类型转换将1转化成布尔值。

if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition2 is true
} else {
  // code to execute if neither condition1 nor condition2 is true
}

也可以使用三元运算符来编写更简洁的条件语句:

condition ? valueIfTrue : valueIfFalse;

2.循环语句

2.1 while循环

如:

uint i = 0;
while (i < 10) {
  // do something
  i++;
}

2.2 for循环

for (uint i = 0; i < 10; i++) {
  // do something
}

3.break、continue return、

与java和c++中的相似,简单的描述一下。
break:跳出现有的循环
continue:退出当前的循环,跳到下一次的循环开始
return:从函数/方法中返回