dart学习记录4(循环、分钟、错误处理)

1. for循环和for ...in...

和大多数语言一样

dart 复制代码
void main() {
  for (int i = 0; i < 5; i++) {
    print(i);
  }
}
dart 复制代码
void main() {
  List<String> fruits = ['apple', 'banana', 'cherry'];
  for (String fruit in fruits) {
    print(fruit);
  }
}

2. while和do-while

和大多数语言一样

dart 复制代码
while (!isDone()) {
  doSomething();
}

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

3.Break和Continue

和大多数语言一样

4.标签(Labels)

语法:lablename :标识符后跟冒号,以将其放在语句之前以创建带标签的语句。循环和 switch 语句通常用作带标签的语句。带标签的语句可以在 break 或 continue 语句中被引用

  • break labelName; 终止带标签语句的执行。这在嵌套循环中打破特定外层循环时很有用
  • break labelName; 终止带标签语句的执行。这在嵌套循环中打破特定外层循环时很有用。
    示例
dart 复制代码
outerLoop:
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    print('i = $i, j = $j');
    if (i == 2 && j == 2) {
      break outerLoop;
    }
  }
}
print('outerLoop exited');
====================================
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
outerLoop exited

在上面的示例中,当 i == 2 且 j == 2 时,break outerLoop; 语句停止了内外两层循环。如果不使用标签,break只能打破内层的for循环。

dart 复制代码
outerLoop:
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    if (i == 2 && j == 2) {
      continue outerLoop;
    }
    print('i = $i, j = $j');
  }
}
================================
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

在上面的示例中,当 i == 2 且 j == 2 时,continue outerLoop; 跳过了 i = 2 的剩余迭代。如果加标签直接continue 只能跳过j=2时的迭代,加了标签跳过了 i = 2 的剩余迭代。

5.if语句

和大多数语言一样

dart 复制代码
if (isRaining()) {
  you.bringRainCoat();
} else if (isSnowing()) {
  you.wearJacket();
} else {
  car.putTopDown();
}

6. If-case语句

类似于模式匹配 ,如果模式与值匹配,则分支将在模式定义的变量范围内执行

dart 复制代码
if (pair case [int x, int y]) {
  print('Was coordinate array $x,$y');
} else {
  throw FormatException('Invalid coordinates.');
}


var list = (3, 3.14, "hello");
  if (list case (int x , double y))
    print("x = $x, y = $y");		//判断为假,不会输出
    
  if (list case (int x , double y, String z))
    print("x = $x, y = $y, z = $z");	判断为真,输出

7.switch语句

dart中非空的 case 子句在完成后会跳转到 switch 的末尾,不需要 break 语句。其他有效的非空 case 子句结束方式包括 continue、throw 或 return 语句

dart 复制代码
switch (command) {
  case 'OPEN':
    executeOpen();
    continue newCase; // 继续执行 newCase 标签处。

  case 'DENIED': // 空 case 落入下一个。
  case 'CLOSED':
    executeClosed(); // 为 DENIED 和 CLOSED 运行,

  newCase:
  case 'PENDING':
    executeNowClosed(); // 为 OPEN 和 PENDING 运行。
  default:
  	executeUnknown();
}

8.switch表达式

case后接表达式

dart 复制代码
// 其中 slash、star、comma、semicolon 等是常量变量...
switch (charCode) {
  case slash || star || plus || minus: // 逻辑或模式
    token = operator(charCode);
  case comma || semicolon: // 逻辑或模式
    token = punctuation(charCode);
  case >= digit0 && <= digit9: // 关系和逻辑与模式
    token = number();
  default:
    throw FormatException('Invalid');
}

看起来牛逼一点的写法

dart 复制代码
token = switch (charCode) {
  slash || star || plus || minus => operator(charCode),
  comma || semicolon => punctuation(charCode),
  >= digit0 && <= digit9 => number(),
  _ => throw FormatException('Invalid'),
};

9.异常和断言

https://dart.dev/language/error-handling 没啥说的,看官方文档

相关推荐
MonkeyKing2 天前
三棵树彻底拆解(Widget / Element / RenderObject)
flutter·dart
哈撒Ki4 天前
快速入门 Dart 语言
前端·flutter·dart
天渺工作室7 天前
Flutter 版的 NVM——FVM 使用指南
flutter·dart
Nathan202406168 天前
Flutter - InheritedWidget
flutter·dart
空中海9 天前
2.3 组件复用与组合
flutter·dart
空中海12 天前
3.3 第三方框架
flutter·dart
空中海13 天前
2.7 列表与滚动性能优化
flutter·性能优化·dart
空中海13 天前
2.4 绘制与动画
flutter·dart
空中海13 天前
2.6 表单与输入处理
flutter·dart
空中海14 天前
1.1 Flutter 简介与架构原理
flutter·dart