Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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.