Dart Controll Flow

In Dart, control statements allow smooth flow of the program. By using the control flow statements, a Dart program can be altered, redirected, or repeated based on the application logic. Dart adopts common control flow statements such as if/else, switch, while, do-while and for loops.

There are the conditional statements: if — else and switch — case, and the iterative statements: for, for — in, forEach, while and do — while loops. There are also some ”helper” statements: break, continue and assert. Don’t forget that we’ve also discussed the try — catch and throw statements a while back when we talked about functions.

 

Categories of Flow Statement

In Dart, Control flow statements can be categorised mainly in three following ways.

  • Decision-making statements
  • Looping statements
  • Jump statements

Dart Decision-Making Statements

The Decision-making statements allow us to determine which statement to execute based on the test expression at runtime. Decision-making statements are also known as the Selection statements. In the Dart program, a single or multiple test expression (or condition) can exist, which evaluates Boolean TRUE and FALSE. These results of the expression/condition helps to decide which block of statement (s) will execute if the given condition is TRUE or FALSE.

 

Dart provides the following types of Decision-making statements.

  • If Statement
  • If-else Statements
  • If else if Statement
  • Switch Case Statement

Dart Looping Statements

Dart looping statements are used to execute the block of code multiple-times for the given number of times until it matches the given condition. These statements are also called Iteration statements.

Dart provides following types of the looping statements.

  • Dart for loop
  • Dart for….in loop
  • Dart while loop
  • Dart do while loop

Dart Jump Statements

Jump statements are used to jump from another statement, or we can say that it transfers the execution to another statement from the current statement.

Dart provides following types of jump statements -

  • Dart Break Statement
  • Dart Continue Statement

Example:

Dart supports if, else if, and else, as you'd expect. Here's a standard if statement:

// this checks a single condition, 

// and the "else" block provides a fallback

if (inPortland) {

  print('Bring an umbrella!');

} else {

  print('Check the weather first!');

}

 

// using "else if" allows you to check for multiple conditions

// and if they all evaluate to false, fall back

if (inPortland && isSummer) {

  print('The weather is amazing!');

} else if(inPortland && isAnyOtherSeason) {

  print('Torrential downpour.');

} else {

  print ('Check the weather!');

}

 

// you can use as many "else if" statements as you need to

if (inPortland && isSummer) {

  print('The weather is amazing!');

} else if(inPortland && isSpring) {

  print('Torrential downpour.');

} else if(inPortland && isAutumn) {

  print('Torrential downpour.');

} else if(inPortland && isWinter) {

  print('Torrential downpour.');

} else {

  print ('Check the weather!');

}

 

Example 2:

main(List<String> args) {

  var day = 6;

 

  if (number > 7) {

    print('Not a Week Day');

  } else if (number < 100) {

    print('Week Day');

  } 

}

 


Advertisements