Dart Loop Control Statements (Break and Continue) (original) (raw)

Last Updated : 26 Mar, 2025

Dart supports two types of loop control statements:

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;

**- **Using break inside while loop

**Example :

Dart `

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 of the above Program:

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.

**- **Using break inside do..while loop

**Example :

Java `

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

**- **Using break inside for loop

**Example :

Java `

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;

**- **Using continue inside a while loop

**Example :

Java `

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 of the above Program:

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.

**- **Using continue inside do..while loop

**Example :

Java `

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

**- **Using continue inside the for loop

**Example :

Java `

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

**Conclusion

Loop control statements in Dart are essential for managing the execution of loops based on specific conditions. Here’s a clearer breakdown:

**break Statement

**continue Statement

By effectively utilizing break and continue, you can optimize loops to prevent unnecessary iterations and control the flow better.