dart. 控制流程语句

You can control the flow of your Dart code using any of the following:

if and else
for loops
while and do-while loops
break and continue
switch and case
assert

1:if...else...

这个几乎每个编程语言都有if条件判断

void main() {
   bool isRaining = true;
  if (isRaining) {
    print("true---->");
  }  else {
    print("false---->");
  }
}

当然了中间也可以使用else if()

for循环

void main() {
  for (var i = 0; i < 5; i++) {
   print(i);
  }
}

While and do-while

void main() {
  bool isDone = false;
  while (!isDone) {
    print("haha");
  }
}

do-while

void main() {
  int a = 10;
  do {
   a--;
   print(a);
  } while (a>=0);
}

break  终止循环

void main() {
  test();
}

void test(){
  for(int i = 0;i<10;i++){
     if(i==5){
       break;
     }
     print(i);
  }
}

continue 跳过本次条件还会执行

void main() {
  test();
}

void test(){
  for(int i = 0;i<10;i++){
     if(i==5){
       continue;
     }
     print(i);
  }
}

Switch and case

Switch statements in Dart compare integer, string, or compile-time constants using ==. The compared objects must all be instances of the same class (and not of any of its subtypes), and the class must not override ==Enumerated types work well in switch statements.

翻译:

Dart中的Switch语句使用==比较整数、字符串或编译时常量。被比较的对象必须都是同一个类的实例(而不是它的任何子类型),并且类不能覆盖==。枚举类型在switch语句中工作得很好

void main() {
  var command = 'OPEN';
  switch (command) {
    case 'CLOSED':
     print("CLOSED----");
      break;
    case 'PENDING':
      print("PENDING----");
      break;
    case 'APPROVED':
      print("APPROVED----");
      break;
    case 'DENIED':
      print("DENIED----");
      break;
    case 'OPEN':
      print("OPEN----");
      break;
    default:
      print("default----");
  }
}

感觉没啥好说的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值