×

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 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.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

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 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 Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute 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 Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 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 Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.