📜  Dart – 循环控制语句(Break 和 Continue)

📅  最后修改于: 2021-09-02 05:34:25             🧑  作者: Mango

Dart支持两种类型的循环控制语句:

  1. 中断声明
  2. 继续声明

中断声明:

此语句用于中断循环的控制流,即如果它在循环中使用,则无论何时遇到它都会终止循环。它会将控制流带出最近的循环。

句法:

break;

示例 1:在 while 循环中使用 break

void main()
{
    int count = 1;
  
    while (count <= 10) {
        print("Geek, you are inside loop $count");
        count++;
  
        if (count == 4) {
            break;
        }
    }
    print("Geek, you are out of while loop");
}

输出:

Geek, you are inside loop 1
Geek, you are inside loop 2
Geek, you are inside loop 3
Geek, you are out of while loop

解释:
最初计数值为 1,当它进入循环时,检查条件 1 <= 10 并且当它为真时,语句打印变量增加,然后检查条件, 2 == 4,这是false 。然后再次循环,直到遇到条件 4 == 4 并且流程退出循环,然后执行最后一个打印语句。

示例 2:在 do..while 循环中使用 break

void main()
{
    int count = 1;
  
    do {
        print("Geek, you are inside loop $count");
        count++;
  
        if (count == 5) {
            break;
        }
    } while (count <= 10);
    print("Geek, you are out of do..while loop");
}

输出:

Geek, you are inside loop 1
Geek, you are inside loop 2
Geek, you are inside loop 3
Geek, you are inside loop 4
Geek, you are out of do..while loop

示例 3:在 for 循环中使用 break

void main()
{
    for (int i = 1; i <= 10; ++i) {
        if (i == 2)
            break;
  
        print("Geek, you are inside loop $i");
    }
  
    print("Geek, you are out of loop");
}

输出:

Geek, you are inside loop 1
Geek, you are out of loop

继续声明:

中断用于结束控制流,而继续则用于继续控制流。当在循环中遇到 continue 语句时,它不会终止循环,而是将流程跳转到下一次迭代。

句法:

continue;

示例 1:在 while 循环中使用 continue

void main()
{
    int count = 0;
  
    while (count <= 10) {
        count++;
  
        if (count == 4) {
            print("Number 4 is skipped");
            continue;
        }
  
        print("Geek, you are inside loop $count");
    }
  
    print("Geek, you are out of while loop");
}

输出:

Geek, you are inside loop 1
Geek, you are inside loop 2
Geek, you are inside loop 3
Number 4 is skipped
Geek, you are inside loop 5
Geek, you are inside loop 6
Geek, you are inside loop 7
Geek, you are inside loop 8
Geek, you are inside loop 9
Geek, you are inside loop 10
Geek, you are inside loop 11
Geek, you are out of while loop

解释:
这里循环的控制流将变得顺畅,但是当计数值变为 4 时,if 条件变为真,并且由于 continue 和下一次迭代跳过数字 4 而跳过下面的语句。

示例 2:在 do..while 循环中使用 continue

void main()
{
    int count = 0;
  
    do {
        count++;
  
        if (count == 4) {
            print("Number 4 is skipped");
            continue;
        }
  
        print("Geek, you are inside loop $count");
    } while (count <= 10);
    print("Geek, you are out of while loop");
}

输出:

Geek, you are inside loop 1
Geek, you are inside loop 2
Geek, you are inside loop 3
Number 4 is skipped
Geek, you are inside loop 5
Geek, you are inside loop 6
Geek, you are inside loop 7
Geek, you are inside loop 8
Geek, you are inside loop 9
Geek, you are inside loop 10
Geek, you are inside loop 11
Geek, you are out of while loop

示例 3:在 for 循环中使用 continue

void main()
{
    for (int i = 1; i <= 10; ++i) {
  
        if (i == 2) {
            print("Geek, you are inside loop $i");
            continue;
        }
    }
  
    print("Geek, you are out of loop");
}

输出:

Geek, you are inside loop 2
Geek, you are out of loop