×

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program.

Dart supports two types of loop control statements:

  • Break Statement
  • Continue Statement

Break Statement :

The ‘ break ’ statement is used to terminate out the regular flow of the program. It this statement is used within a loop then as soon as the compiler encounters this statement, it terminates the loop. The flow will transfer to the immediate next statement after the loop.

Consider the syntax of using break statement :

Syntax:

break ;

Consider the following example in Dart that explains the concept of break statement.

Example 1 : Using break inside while loop

void main( )
{
    int ctr = 1 ;
  
    while ( ctr <= 5 ) 
    {
        print( " You are inside the loop, right now ! " ) ;
        print( ctr ) ;
        ctr ++ ;
  
        if ( ctr == 5 ) 
        {
            break ;
        }
    }
    print( " Now you are out of the loop because of break statement ! " ) ;
}

Output :

You are inside the loop, right now ! 
1
 You are inside the loop, right now ! 
2
 You are inside the loop, right now ! 
3
 You are inside the loop, right now ! 
4
 Now you are out of the loop because of break statement!

Explanation :

Initially the value of counter variable ‘ ctr ’ is 1 and before transferring the flow inside the loop, a condition is checked. Till ( ctr <= 4 ), the control remains inside the loop and the print( ) statement is executed. When ( ctr == 4 ), the compiler encounters the break statement, the flow comes out of the loop and the statement immediately after the loop gets executed.

Consider the following example that explains the concept ofs Dart do… while loop

Example 2 : Using break inside do..while loop

void main( )
{
    int ctr = 1 ;
    do 
       {
        print( " You are inside the loop, right now ! " ) ;
        print( ctr ) ;
        ctr ++ ;
          if ( ctr == 4 ) 
       {
            break ;
        }
    } while ( ctr <= 5 ) ;
    print( " Now you are out of the loop because of break statement ! " ) ;
}

Output :

You are inside the loop, right now ! 
1
 You are inside the loop, right now ! 
2
 You are inside the loop, right now ! 
3
 Now you are out of the loop because of break statement ! 

Continue Statement:

As you have studied so far, the ‘ break ’ statement is used to terminate the flow out of the loop. The ‘ continue ’ statement has opposite functionality, it 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.

Consider the syntax of using continue statement :

Syntax :

continue ;

Example 1 : Using continue inside while loop

void main( )
{
    int ctr = 0 ;
  
    while ( ctr <= 10 ) 
    {
        ctr ++ ;
  
        if ( ctr == 4 ) 
        {
            print( " Now you are inside the loop. 4 is printed. " ) ;
            continue ;
        }
  
        print( " You are still in the loop. Here is the counter updation : " ) ;
        print( ctr ) ;
    }
    print( " You are out of the while loop ! " ) ;
}

Output :

You are still in the loop. Here is the counter updation : 
 1
 You are still in the loop. Here is the counter updation : 
 2
 You are still in the loop. Here is the counter updation : 
 3
 Now you are inside the loop. 4 is printed. 
 You are still in the loop. Here is the counter updation : 
 5
 You are still in the loop. Here is the counter updation : 
 6
 You are still in the loop. Here is the counter updation : 
 7
 You are still in the loop. Here is the counter updation : 
 8
 You are still in the loop. Here is the counter updation : 
 9
 You are still in the loop. Here is the counter updation : 
 10
 You are still in the loop. Here is the counter updation : 
 11
 You are out of the 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 ctr = 0 ;
  
    do {
        ctr ++ ;
  
        if ( ctr == 4 ) 
        {
            print( " Now you are inside the loop. 4 is printed. " ) ;
            continue ;
        }
      
        print( " You are still in the loop. Here is the counter updation : " ) ;
        print( ctr ) ;
      
    } while ( ctr <= 10 ) ;
  
    print( " You are out of while loop. " ) ;
}

Output :

 You are still in the loop. Here is the counter updation : 
 1
 You are still in the loop. Here is the counter updation : 
 2
 You are still in the loop. Here is the counter updation : 
 3
 Now you are inside the loop. 4 is printed. 
 You are still in the loop. Here is the counter updation : 
 5
 You are still in the loop. Here is the counter updation : 
 6
 You are still in the loop. Here is the counter updation : 
 7
 You are still in the loop. Here is the counter updation : 
 8
 You are still in the loop. Here is the counter updation : 
 9
 You are still in the loop. Here is the counter updation : 
 10
 You are still in the loop. Here is the counter updation : 
 11
 You are out of while loop.

Related Topics

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.