码迷,mamicode.com
首页 > 其他好文 > 详细

[Dart语法]第五章:控制流程语句

时间:2021-02-16 11:58:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:com   lap   with   简写   控制流   wro   错误提示   执行   for循环   

控制流程语句

if-else语句

//标准语句
bool isWrong = true;
bool isTesting = false;
if (isWrong) {
    print(‘错误提示‘);
} else if (isTesting) {
    print(‘正在测试‘);
} else {
    print(‘其他‘);
}

//简写
bool isWrong = true;
if (isWrong) print(‘错误‘);

for循环

//for loop
List list = [1, 2, 3, 4, 5];
for (int i = 0; i < list.length; i++) {
    print(list[i]);
}

//forEach
List list = [1, 2, 3, 4, 5];
list.forEach((element) {
    print(element);
});

//for-in
List list = [1, 2, 3, 4, 5];
for (var i in list) {
    print(i);
}

while和do-while

//while
List list = [1, 2, 3, 4, 5];
int i = 0;
while (i < list.length) {
    print(list[i]);
    i++;
}

//do-while
List list = [1, 2, 3, 4, 5];
int i = 0;
do {
    print(list[i]);
    i++;
} while (i < list.length);

switch-case

switch (1) {
    case 1:
        print(‘books‘);
        break;
    case 2:
        print(‘pen‘);
        break;
    case 3:
        print(‘laptop‘);
        break;
    case 4:
        print(‘phone‘);
        break;
    default:
        print(‘other‘);
}

assert

String urlString = ‘http://www.baidu.com‘;
assert(urlString.startsWith(‘https‘),
       ‘URL ($urlString) should start with "https".‘);
// 当urlString不是以https开头时,代码的执行会被打断
// 当urlString是以https开头时,代码会继续执行

[Dart语法]第五章:控制流程语句

标签:com   lap   with   简写   控制流   wro   错误提示   执行   for循环   

原文地址:https://www.cnblogs.com/TobuTobu/p/14398752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!