美文网首页
Dart 2 (五) 流程控制

Dart 2 (五) 流程控制

作者: pstommy | 来源:发表于2018-09-13 12:47 被阅读0次

流程控制

您可以使用以下任何一种方法来控制DART代码的流程:

  • if and else
  • for 循环
  • while 和 do-while 循环
  • break 和 continue
  • switch 和 case
  • assert

您还可以使用try-catch和throw来影响控制流,作为 Exceptions 中解释的。

If 和 else

Dart支持if语句和可选的else语句

if (isRaining()) {
  you.bringRainCoat();
} else if (isSnowing()) {
  you.wearJacket();
} else {
  car.putTopDown();
}

与JavaScript不同的是,条件必须使用布尔值,其他都不行

For 循环

您可以使用for循环标准进行遍历。例如:

var message = StringBuffer('Dart is fun');
for (var i = 0; i < 5; i++) {
  message.write('!');
}

Dart的for循环中的闭包捕获了索引的值,避免了JavaScript中常见的陷阱。例如

var callbacks = [];
for (var i = 0; i < 2; i++) {
  callbacks.add(() => print(i));
}
callbacks.forEach((c) => c());

输出是0,然后是1。相反,示例将用JavaScript将打印2和2。
如果要遍历的对象是可遍历的,那么可以使用forEach()方法。如果您不需要知道当前的遍历计数器,那么使用forEach()是一个很好的选择:

candidates.forEach((candidate) => candidate.interview());

可遍历类,如List和Set也支持for-in形式的遍历:

var collection = [0, 1, 2];
for (var x in collection) {
  print(x); // 0 1 2
}

While 和 do-while

while循环计算循环之前的条件:

while (!isDone()) {
  doSomething();
}

do-while循环在循环之后评估条件:

do {
  printLine();
} while (!atEndOfPage());

Break 和 continue

使用break 中断循环

while (true) {
  if (shutDownRequested()) break;
  processIncomingRequests();
}

使用continue跳到下一个循环遍历:

for (int i = 0; i < candidates.length; i++) {
  var candidate = candidates[i];
  if (candidate.yearsExperience < 5) {
    continue;
  }
  candidate.interview();
}

如果您使用可遍历的List或set,您可能会以不同的方式编写该示例:

candidates
    .where((c) => c.yearsExperience >= 5)
    .forEach((c) => c.interview());

Switch 和 case

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

var command = 'OPEN';
switch (command) {
  case 'CLOSED':
    executeClosed();
    break;
  case 'PENDING':
    executePending();
    break;
  case 'APPROVED':
    executeApproved();
    break;
  case 'DENIED':
    executeDenied();
    break;
  case 'OPEN':
    executeOpen();
    break;
  default:
    executeUnknown();
}

下面的示例省略了case子句中的break语句,从而生成一个错误:

var command = 'OPEN';
switch (command) {
  case 'OPEN':
    executeOpen();
    // ERROR: Missing break

  case 'CLOSED':
    executeClosed();
    break;
}

然而,Dart确实支持空case,允许一种形式:

var command = 'CLOSED';
switch (command) {
  case 'CLOSED': // Empty case falls through.
  case 'NOW_CLOSED':
    // Runs for both CLOSED and NOW_CLOSED.
    executeNowClosed();
    break;
}

如果你真的想要跳空,你可以使用一个continue语句和一个标签:

var command = 'CLOSED';
switch (command) {
  case 'CLOSED':
    executeClosed();
    continue nowClosed;
  // Continues executing at the nowClosed label.

  nowClosed:
  case 'NOW_CLOSED':
    // Runs for both CLOSED and NOW_CLOSED.
    executeNowClosed();
    break;
}

case子句可以有局部变量,这些变量只能在该子句的作用域内可见。

Assert

如果布尔条件为false,则使用assert语句中断正常执行。您可以在本教程中找到assert语句的示例。以下是一些:

// Make sure the variable has a non-null value.
assert(text != null);

// Make sure the value is less than 100.
assert(number < 100);

// Make sure this is an https URL.
assert(urlString.startsWith('https'));

注意:assert 语句对生产代码没有影响;它们只用于开发。Flutter允许在调试模式中使用断言。支持的工具有dartdevc,通常默认支持断言。一些工具,比如dart和dart2js,通过命令行标志支持断言:--enable-asserts.
要将消息附加到断言,需要添加一个字符串作为第二个参数

assert(urlString.startsWith('https'),
    'URL ($urlString) should start with "https".');

要assert的第一个参数可以是解析为布尔值的任何表达式。如果表达式的值为true,则断言成功并继续执行。如果为false,则断言失败并抛出异常(AssertionError)。

相关文章

  • Dart 2 (五) 流程控制

    流程控制 您可以使用以下任何一种方法来控制DART代码的流程: if and else for 循环 while ...

  • Dart(2.2) - 流程控制语句

    流程控制语句 可以使用下面的语句来控制Dart代码的流程: ifand else for loops while ...

  • 【Flutter】Dart基本语法

    Dart编程语言——基本概念及变量类型Dart编程语言——方法Dart编程语言——操作符、流程控制和异常Dart编...

  • Dart学习笔记

    Dart学习笔记 目录·1.推荐学习网站·2.认识dart·3.语言特性·4.变量与常量·5.关键字·6.流程控制...

  • Dart基础(三)-流程控制语句

    简介:   Dart中的流程控制语句: if and else for loops while and do-wh...

  • 06Dart语言基础-流程控制

    Dart中的流程控制几乎与别的语言没有任何差别 1. if 2.Switch 3. for 4. while 和 ...

  • flutter Dart语法

    Dart语言特点: 打印语法: 一、变量与常量 二、数据类型 三、函数 四、运算符 五、控制流程 六、类 七、泛型...

  • Dart - 流程控制语句

    Dart的控制流程语句跟其他编程语言类似,这里就不一一记录了,只记录需要注意的特殊点。 if Dart 的 if ...

  • (五)Dart流程控制语句、断言、异常

    一、Control flow statements(流程控制语句) ifandelse for while and...

  • Dart(3)流程控制

    if 和 else dart的if else 必须是bool型 循环 for,while 和 do-while 不...

网友评论

      本文标题:Dart 2 (五) 流程控制

      本文链接:https://www.haomeiwen.com/subject/tjrbgftx.html