5.Control flow statements-流程控制(Dart中文文档)
你可以使用如下流程控制符:
- if and else
- for loops
- while and do-while loops
- break and continue
- switch and case
- assert
同时,你可以用try-catch 和throw去跳出流程控制逻辑,并在异常代码块中进行处理。
If and else
下面是if和else配合使用的示例:
if (isRaining()) {
you.bringRainCoat();
} else if (isSnowing()) {
you.wearJacket();
} else {
car.putTopDown();
}
有点要注意,Dart语言不像JavaScript,if判断必须是Boolean类型对象,不能将null看作false
For loops
for循环示例:
var message = StringBuffer('Dart is fun');
for (var i = 0; i < 5; i++) {
message.write('!');
}
在for循环中局部变量再闭包函数中使用,变量值将会是当时的快照值,后续i变动,也不会改变。这个和JavaScript不同。
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
callbacks.forEach((c) => c());
上面的代码,将先输出0,再输出1。而在JavaScript中,都是输出2,这就是两个之间的一个差异
对一个数组对象循环时,如果你不需要知道循环的计数器,可以用forEach写法。
candidates.forEach((candidate) => candidate.interview());
数组或者集合也可以用for-in的写法做循环。
var collection = [0, 1, 2];
for (var x in collection) {
print(x); // 0 1 2
}
While and do-while
while是前置判断的循环写法:
while (!isDone()) {
doSomething();
}
do-while 是后置判断的循环写法:
do {
printLine();
} while (!atEndOfPage());
Break and 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();
}
如果你是对数组或者集合操作,可以用如下写法:
candidates
.where((c) => c.yearsExperience >= 5)
.forEach((c) => c.interview());
Switch and case
switch可以用数值,字符串,编译常量作为判断值,case后面的对象必须在类中初始化,不能在父类中,通过该对象的类不能重载==。另外,枚举类型也可以作为判断条件。
非空的case,必须使用break,coutinue,throw,return 结束,否则将编译错误。
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();
}
Dart的非空case必须break,除非你采用coutinue进行跳转。
var command = 'OPEN';
switch (command) {
case 'OPEN':
executeOpen();
// ERROR: Missing break
case 'CLOSED':
executeClosed();
break;
}
Dart支持空的case,处理逻辑和其后续case相同。
var command = 'CLOSED';
switch (command) {
case 'CLOSED': // Empty case falls through.
case 'NOW_CLOSED':
// Runs for both CLOSED and NOW_CLOSED.
executeNowClosed();
break;
}
如果在非空的case中,你希望switch继续向下判断,可以用continue +label来实现。
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
使用assert用来在false条件下,抛出异常来中断程序执行。
// 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可以通过debug模式启用assert,IDE上面,只有dartdevc默认支持dart,其它工具,比如dart,dart2js,需要用--enable-asserts来开启assert
assert的带2个参数写法:
assert(urlString.startsWith('https'),
'URL ($urlString) should start with "https".');
第六篇准备翻译 Exceptions 异常
5.Control flow statements-流程控制(Dart中文文档)的更多相关文章
- 4.Operators-操作符(Dart中文文档)
Dart有如下操作符: Description Operator unary postfix expr++ expr-- () [] . ?. unary prefix -expr !expr ~ex ...
- 2.Built-in types-基本数据类型(Dart中文文档)
初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. Dart语言内置如下数据类型: numbers strings booleans lists (所谓的数组) maps ...
- 1.Variables-变量(Dart中文文档)
初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. 如下是变量定义和赋值的示例 var name = 'Bob'; 变量存储的是一个引用地址.如上的变量name指向了一个值 ...
- 8.Generics 泛型(Dart中文文档)
这篇翻译的不好 如果你看API文档中的数组篇,你会发现类型一般写成List.<...>的写法表示通用类型的数组(未明确指定数组中的数据类型).通常情况泛型类型用E,T,S,K,V表示. W ...
- 7.Classes-类(Dart中文文档)
Dart是一个面向对象的语言,同时增加了混入(mixin)继承的特性.对象都是由类初始化生成的,所有的类都由Object对象继承.混入继承意味着尽管所有类(除了Object类)只有一个父类,但是类的代 ...
- 6.Exceptions-异常(Dart中文文档)
异常是用于标识程序发生未知异常.如果异常没有被捕获,If the exception isn't caught, the isolate that raised the exception is su ...
- 3.Functions-函数(Dart中文文档)
初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. Dart是完全的面向对象的语言,甚至函数也是一个Function类型的对象.这意味着函数可以赋值给变量或者作为函数的参数 ...
- Flutter 中文文档网站 flutter.cn 正式发布!
在通常的对 Flutter 介绍中,最耳熟能详的是下面四个特点: 精美 (Beautiful):充分的赋予和发挥设计师的创造力和想象力,让你真正掌控屏幕上的每一个像素. ** 极速 (Fast)**: ...
- Apache Spark 2.2.0 中文文档
Apache Spark 2.2.0 中文文档 - 快速入门 | ApacheCN Geekhoo 关注 2017.09.20 13:55* 字数 2062 阅读 13评论 0喜欢 1 快速入门 使用 ...
随机推荐
- 11 tensorflow在tf.while_loop循环(非一般循环)中使用操纵变量该怎么做
代码(操纵全局变量) xiaojie=1 i=tf.constant(0,dtype=tf.int32) batch_len=tf.constant(10,dtype=tf.int32) loop_c ...
- 2.Spring——maven依赖
1.spring-core 2.spring-context 3.spring-orm 4.spring-web spring-webmvc others pmo demo1 pmo demo2 1. ...
- mvc5中重命名项目的名称后,出现"找到多个与名为“Home”的控制器匹配的类型"
1.已把项目中所有的Webapplication1改为了MvcMovie,但是运行后,还是报错: 找到多个与名为“Home”的控制器匹配的类型 2.已重新生成解决方安,还是不行. 解决方法:把bin文 ...
- 自己搭建anki服务器
目录 centos端 电脑客户端 安卓端 centos端 # 安装服务 yum -y install python-setuptools easy_install Ankiserver mkdir - ...
- Linux系统管理员命令:sudo
sudo是个统管一切的命令.它的字面意思是代表“超级用户才能做!”(super user do!)对Linux系统管理员或高级用户而言,它是必不可少的最重要的命令之一.你可曾有过这样的经历:在终端中试 ...
- [IIS] [PHP] 500.19 随机出现
微信确认有BUG: https://support.microsoft.com/en-au/help/3007507/-http-error-500.19-error-when-you-browse- ...
- [WinCE | VS2008 | Solution] VS2008 building WinCE projects taking a long time
1. Open C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CompactFramework.Common.targets 2. Find pa ...
- MySQL Group Replication配置
MySQL Group Replication简述 MySQL 组复制实现了基于复制协议的多主更新(单主模式). 复制组由多个 server成员构成,并且组中的每个 server 成员可以独立地执行事 ...
- POST请求上传多张图片并携带参数
POST请求上传多张图片并携带参数 在iOS中,用POST请求携带参数上传图片是非常恶心的事情,HTTPBody部分完全需要我们自己来配置,这个HTTPBody分为3个部分,头部分可以携带参数,中间部 ...
- oracle 数据库数据备份
oracle 数据库数据备份 1.使用oracle用户应该就可以进行数据备份(不需要root用户):su oracle 查oracle实例名:echo $ORACLE_SID 例如查出来的 ...