Skip to content
Related Articles

Related Articles

Dart – Loop Control Statements (Break and Continue)
  • Last Updated : 18 Jun, 2020

Dart supports two types of loop control statements:

  1. Break Statement
  2. Continue Statement

Break Statement:

This statement is used to break the flow of control of the loop i.e if it is used within a loop then it will terminate the loop whenever encountered. It will bring the flow of control out of the nearest loop.

Syntax:

break;

Example 1: Using break inside while loop




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");
}

Output:



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

Explanation:
Initially count value is 1, as it goes inside loop the condition is checked, 1 <= 10 and as it is true the statement is printed variable is increased and then condition is checked, 2 == 4, which is false. Then the loop is followed again till the condition 4 == 4 is encountered and the flow comes out of the loop and then last print statement is executed.

Example 2: Using break inside do..while loop




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");
}

Output:

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

Example 3: Using break inside for loop




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");
}

Output:

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

Continue Statement:

While the break is used to end the flow of control, continue on the other hand is used to continue the flow of control. When a continue statement is encountered in a loop it doesn’t terminate the loop but rather jump the flow to next iteration.

Syntax:

continue;

Example 1: Using continue inside while loop




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");
}

Output:

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

Explanation:
Here control flow of the loop will go smooth but when count value becomes 4 the if condition becomes true and the below statement is skipped because of continue and next iteration skipping number 4.

Example 2: Using continue inside do..while loop




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");
}

Output:

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

Example 3: Using continue inside for loop




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");
}

Output:

Geek, you are inside loop 2
Geek, you are out of loop
My Personal Notes arrow_drop_up
Recommended Articles
Page :