Control flow statements

We can control Dart the flow of your code using any of the following

  • if and else
  • for loops
  • while and do-while loops
  • break and continue
  • switch and case
  • assert

we can influence the control flow use try-catchand throw

If and else

Dart Support if statements and optional else statement, as the following example shows

if (isVisible()) {
  //Some Logic
} else if (isSnowing()) {
  //Some Logic
} else {
  //Some Logic
}

Unlike the JavaScript, the condition must be booleana value, the other not

Dart If  Statement Example

void main() { 
   var  num=5; 
   if (num>0) { 
      print("number is positive"); 
   }    
}

 

Dart If Else Example

void main() { 
   var num = 12; 
   if (num % 2==0) { 
      print("Even"); 
   } else { 
      print("Odd"); 
   } 
}

 

For loops

The for loop is an implementation of a definite loop. The for loop executes the code block for a specified number of times. It can be used to iterate over a fixed set of values, such as an array.

Following is the syntax of the for loop

for (initial_count_value; termination-condition; step) { 
   //statements 
} 

 

Dart for loop Example

void main() { 
   var num = 5; 
   var factorial = 1; 
   
   for( var i = num ; i >= 1; i-- ) { 
      factorial *= i ; 
   } 
   print(factorial); 
}

 

Dart The closure for cycle index value captured, avoiding similar in 'JavaScript' in the trap for example

var data = [];
for (var i = 0; i < 2; i++) {
  data.add(() => print(i));
}
callback.forEcha((c) => c());

As expected, the first output 0, then the 1contrary, in JavaScript will always output of this example 2.
If you object to iterate is iterative, you can use the forEach()method. Use forEach ()is a good option if you do not need to know the current iteration counter

students.forEach((candidate) => student.interview());

For example, List and Set iterative class also supports for-initerative form

var collection = [0, 1, 2];
for (var x in collection) {
  print(x); // 0 1 2
}

 

While and do-while

while The loop judgment condition is before the loop

while (!isDone()) {
  doSomething();
}

 

do-while Loop after judging the condition:

do {
  printLine();
} while (!atEndOfPage());

 

Break and continue
Use breakto end the cycle

while (true) {
  if (shutDownRequested()) break;
  processIncomingRequests();
}

Use continueto skip into the next cycle

for (int i = 0; i < students.length; i++) {
  var student = students[i];
  if (student.yearsExperience < 5) {
    continue;
  }
  student.interview();
}

 

Switch and case

Switch Statement Dartin Comparative interger stringuse or compile-time constants ==. For comparison must be of the same class (subclass which is not), and can not cover type ==,the Enumerated types in switchgood running.

Note: In Dartthe switch statement for a particular environment, for example, interpreters or scanners

Every non-empty case conditions to break finish with the statement as a specification. Other effective methods to a non-empty case condition continue throwor returnstatements.

Used defaultto execute the code when there are no conditions case matching conditions

var command = 'OPEN';
switch (command) {
  case 'CLOSED':
    executeClosed();
    break;
  case 'PENDING':
    executePending();
    break;
  case 'APPROVED':
    executeApproved();
    break;
  case 'DENIED':
    executeDenied();
    break;
  case 'OPEN':
    executeOpen();
    break;
  default:
    executeUnknown();
}

 

The following example is omitted breakstatement caseconditions, then an error has occurred

var command = 'OPEN';
switch (command) {
  case 'OPEN':
    executeOpen();
    // ERROR: Missing break

  case 'CLOSED':
    executeClosed();
    break;
}

 

Assert
Use the assert statement to terminate the normal execution if the boolean condition that false you can find examples of assert statements

// Make sure the variable has a non-null value.
assert(text != null);

// Make sure the value is less than 100.
assert(number < 100);

// Make sure this is an https URL.
assert(urlString.startsWith('https'));

 

Note: Assert statement has no role in the production code; valid only in the development environment. Flutter In debug mode can be used in development tools such as assert. Dart devc usually supported by default assert. Some tools, for example: dart and dart2js support asserts via command line flags:--enable-asserts


To assertadd a message, a text can be added to the second argument

assert(urlString.startsWith('https'),
    'URL ($urlString) should start with "https".');

 

assert The first parameter can be any condition that resolves to a Boolean value. If the condition value is true, the assertion succeeds and execution continues. If it is false, the assertion fails and an exception is thrown