控制流程语句
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开头时,代码会继续执行