通过前面的基础知识了解学习,咱们今天能够进入语句模块啦。python
dart主要有如下流程控制语句:学习
(一)if-elselua
dart的if(或者else if)的条件表达式必须为bool表达式,不能使用其余类型。dart的if-else用法与C语言相似,再也不细述。spa
int a = 6; if(a<0) { print("aaa"); } else if((a>=0) && (a<=3)) { print("bbb"); } else { print("ccc"); }
(二) for循环debug
与C语言系的for循环用法相同,再也不细述。补充一点:对于List和Set等可迭代类型,也能够使用for-in格式去迭代(有点像python),看个例子:code
var collection = [0, 1, 2]; for (var x in collection) { print(x); // 0 1 2 }
就是这样。server
(三) while和do-whileblog
这两个也再也不细述了,和C语言同样。(while循环是先判条件再执行动做;do-while是先执行动做再判循环条件)。get
(四)break和continueit
与C语言同样,break是跳出当前循环,continue是跳过当次循环的剩余语句,继续开始新一次的循环。
(五)switch-case
与C语言相似,再也不细述。一点特例,dart在switch-case里支持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; }
(六)assert
若是布尔条件为false,则会中断执行。assert语句是有两个参数的
assert(condition, optionalMessage); //第二个参数是可选的
第一个参数能够是返回值为bool的表达式,若是表达式的返回值为true,则assert经过且程序继续正常执行; 若是表达式为false,则assertion失败且抛出一个异常。
再贴一段官方的注释,讲了assert能有效工做的场景,你们自行阅读吧
When exactly do assertions work? That depends on the tools and framework you’re using:
--enable-asserts
.In production code, assertions are ignored, and the arguments to assert
aren’t evaluated.