dart語言學習(十三)— 控制流語句
阿新 • • 發佈:2018-12-15
-
條件語句
if 語句
if…else if語句
if…else if…else語句
void main(List<String> args) {
int score = 60;
if (score > 90) {
print("優秀");
} else if (score > 60) {
print("良好");
} else if (score == 60) {
print("及格");
} else {
print("不及格");
}
}
及格
Exited
-
迴圈語句
for迴圈
for…in迴圈
void main(List<String> args) {
var list = [1, 2, 3, 4, 5];
for (var index = 0; index < list.length; index++) {
print(list[index]);
}
print("-----------------------------------------");
for (var item in list) {
print(item);
}
}
1 2 3 4 5 ----------------------------------------- 1 2 3 4 5 Exited
-
while語句
while迴圈
do…while迴圈
void main(List<String> args) {
int count=0;
while(count<5){
print(count++);
}
print("--------------$count---------------");
do{
print(count--);
}while(count>-3&& count<5);
}
0 1 2 3 4 --------------5--------------- 5 4 3 2 1 0 -1 -2 Exited
-
break和continue
終止迴圈:break
跳出當前迴圈:continue
void main(List<String> args) {
var list = [1, 2, 3, 4, 5];
for (var item in list) {
if (item == 2) {
break;
}
print(item);
}
print("------------------------");
for (var item in list) {
if (item == 2) {
continue;
}
print(item);
}
print("------------------------");
var list2=[9,8,7];
for (var item in list) {
for (var item2 in list2) {
if(item2==8){
break;
}
print(item2);
}
}
}
1
------------------------
1
3
4
5
------------------------
9
9
9
9
9
Exited
-
switch…case語句
比較型別:num ,String ,編譯期常量,物件,列舉
非空case必須有一個break
default處理預設情況
continue跳轉標籤(特有,其他語言少見)
void main(List<String> args) {
String language = "H5";
switch (language) {
case "Dart":
print("---------Dart-------");
break;
case "H5":
print("---------H5-------");
break;
default:
print("-----none------");
break;
}
print("-------------------------------");
switch (language) {
case "Dart":
print("---------Dart-------");
break;
case "H5":
print("---------H5-------");
continue D;
D:
case "JAVA":
print("---------JAVA-------");
break;
default:
print("------none-----");
break;
}
}
---------H5-------
-------------------------------
---------H5-------
---------JAVA-------
Exited