Dart – Loop Control Statements (Break and Continue)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.