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 没啥说的,看官方文档

相关推荐
yuanlaile18 小时前
Flutter小白零基础入门到高级项目实战全集
flutter·dart·flutter开发鸿蒙
pengyu5 天前
系统化掌握Flutter开发之导航器(Navigator)(一):页面跳转的“指挥官”
android·flutter·dart
pengyu5 天前
系统化掌握Flutter开发之路由(Route)(一):筑基之旅
android·flutter·dart
北岛贰6 天前
爆肝两个月,我用flutter开发了一款免费音乐app
flutter·dart
pengyu9 天前
系统化掌握Flutter开发之主题(Theme)(一):筑基之旅
android·flutter·dart
pengyu9 天前
系统化掌握Flutter组件之InteractiveViewer:释放你的双手
android·flutter·dart
pengyu10 天前
系统化掌握Flutter组件之Dismissible
android·flutter·dart
pengyu10 天前
系统化掌握Flutter组件之Draggable/DragTarget
android·flutter·dart
moton201711 天前
Flutter开发避坑指南:高频问题排查与性能调优实战
mqtt·flutter·性能优化·前端框架·自动化·dart