# 流程控制 flow

本文作者:阳九五 (opens new window)

本站地址:https://blog.56321654.xyz (opens new window)

# if else

int count = 5;
if ( count > 5) {
    print('hello YoungYang');
}else if( count > 0 ){
    print('hello World');
}else{
    print('hello');
}

hello YoungYang
1
2
3
4
5
6
7
8
9
10

# for

for (var i = 0; i < 3; i++) {
    print(i);
}

0
1
2
1
2
3
4
5
6
7

# while

先判断后执行

int i = 0;
while( i<3 ) {
  print(i++);
}

0
1
2
1
2
3
4
5
6
7
8

# do while

先执行后判断

int i = 0;
do{
    print(i++);
}while( i<3 );

1
2
3
4
5

# switch case

int count = 0;
switch (count) {
    case 0:
        print('Yang');
        break;
    case 1:
        print('YangYang');
        break;
    default:
        print('not find');
};

Yang
1
2
3
4
5
6
7
8
9
10
11
12
13

# break

终止循环

int i = 0;
while( i<5 ) {
  print(i++);
  if( i == 3 ){
    break;
  }
}

0
1
2
1
2
3
4
5
6
7
8
9
10
11

# continue

跳过当前循环,直接进入到下一次循环

int i = 0;
while( i<5 ) {
  print(i++);
  if( i == 3 ){
    continue;
  }
}

0
1
2
4
1
2
3
4
5
6
7
8
9
10
11
12

# continue 指定位置

String command = "close";
switch (command) {
  case "open":
    print("open");
    break;
  case "close":
    print("close");
    continue doClear;
  case "close2":
    print("close2");
    continue doClear;

  doClear:
  case "doClose":
    print("doClose");
    break;

  default:
    print("other");
}


close
doClose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
最近更新: 8/6/2025, 2:39:35 PM