How to use DART control flow statements

In this article we will discuss about different type control flow statements in DART.
  • 2034

Control flow statements

The statements which allow us to control the flow of our program, rather than execution every one of code in the order it appears in the program. Some of control flow statements is given below.

  • Break statement

    The break statement is used when we want to exit out  from any for, while , do-while loops, or any statements like switch etc.

Example of break statement

void main() {

  for(int i=1;i<=3;i++){

  print(i);

  break; //here only single value is printed  because break statements is exit out from it's loop.

  }
}


Output:

break.png

  • Continue statement

    The continue statement is similar to break, it is also used in for, while, do-while, it exit from the current iteration of the loop and  execution will start from beginning of the loop.

Example of continue statement
 

void main() {

  var i;

  for(i=1;i<=3;i++){

    print(i);

    continue;

    i++; //skip that step.

  }
}


Output:

continueDart.png

  • Assert statements

    A assert statements is work like a if statements , it disrupt normal execution if a boolean condition is false.

Example of assert statements
 

void main() {

 bool a = true;

  assert(a!=null);

    print("If you use assert you also compare two instance variables");
}


Output:

AssertDart.png

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.