Flutter(四)Dart语言基础-控制语句
2020-07-17 本文已影响0人
U东东枪
本文主要学习Dart 中控制语句
if 语句
- if 语句
- if...else if 语句
- if...else if...else语句
double score = 90.5;
if (score >= 90) {
if (score == 100) {
print("完美");
} else {
print("优秀");
}
} else if (score > 60) {
print("良好");
} else if (score == 60) {
print("及格");
} else {
print("不及格");
}
//优秀
for 语句
-
for
循环 -
for...in
循环
var list = ["a", "b", "c", "d", "e"];
for (var i = 0; i < list.length; i++) {
print(list[i]);
// a
// b
// c
// d
// e
}
print("-------------");
for (var item in list) {
print(item);
// a
// b
// c
// d
// e
}
while语句
-
while
循环 -
do...while
循环
int a = 1;
while (a < 5) {
print(a++);
}
// 1
// 2
// 3
// 4
//-----------------
int b = 1;
do {
print(++b);
} while (b < 5);
// 2
// 3
// 4
// 5
break
和 continue
- 终止循环:
break
- 跳出当前循环:
continue
var list = [1, 2, 3];
for (var i = 0; i < list.length; i++) {
if (list[i] == 2) {
continue; //跳出本次循环,执行下一个循环
}
print(list[i]);
//1
//3
}
print("-------------");
var list1 = [4, 5, 6];
for (var item in list) {
for (var item1 in list1) {
if (item1 == 5) {
break; // 结束循环
}
print(item1);
//4
//4
//4
}
}
//item1 = 5时,结束循环
switch...case
语句
- 比较类型:
num
,String
,编译期常量
, 对象 , 枚举 - 非空
case
必须有一个break
-
default
处理默认情况 -
continue
跳转标签
String language = "Swift";
switch (language) {
case "Dart":
print("I love Dart");
break;
case "Swift":
print("I love Swift");
break;
case "Java":
print("I love Java");
break;
default:
print("None");
}
//I love Swift
switch (language) {
Test:
case "Dart":
print("I love Dart");
break;
case "Swift":
print("I love Swift");
continue Test;
case "Java":
print("I love Java");
break;
default:
print("None");
}
//I love Swift
//I love Dart