This code may not run directly, just show the syntax.

Dart has the following statements to control the flow of code execution:

  1. ifelse
  2. forcycle
  3. whiledo-whilecycle
  4. breakcontinue
  5. switchcase
  6. assert

ifelse

if (isRaining()) {
  you.bringRainCoat();
} else if (isSnowing()) {
  you.wearJacket();
} else {
  car.putTopDown();
}
Copy the code

forcycle

// for
var message = StringBuffer('Dart is fun');
for (var i = 0; i < 5; i++) {
  message.write('! ');
}

// forEach
var callbacks = [];
for (var i = 0; i < 2; i++) {
  callbacks.add(() => print(i));
}
callbacks.forEach((c) => c());

// for-in
for (var candidate in candidates) {
  candidate.interview();
}
Copy the code

whiledo-whilecycle

// while
while(! isDone()) { doSomething(); }// do-while
do {
  printLine();
} while(! atEndOfPage());Copy the code

breakcontinue


// Use break to stop looping
while (true) {
  if (shutDownRequested()) break;
  processIncomingRequests();
}

// Use continue to skip to the next loop iteration:
for (int i = 0; i < candidates.length; i++) {
  var candidate = candidates[i];
  if (candidate.yearsExperience < 5) {
    continue;
  }
  candidate.interview();
}
Copy the code

switchcase

The Switch statement uses == in Dart to compare integers, strings, or compile-time constants. The two objects must be of the same type and cannot be subclasses and the == operator is not overridden. Enumerated types are ideal for use in Switch statements.

Each non-empty case clause must have a break statement. Non-empty case statements can also be terminated with a continue, throw, or return statement. If no case statement is matched, the code in the default clause is executed:


// Use templates normally
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();
}

// An error message is displayed when the break statement is omitted.
var command = 'OPEN';
switch (command) {
  case 'OPEN':
    executeOpen();
    // ERROR: Missing break

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

// Support empty case statements that can be executed fall-through
var command = 'CLOSED';
switch (command) {
  case 'CLOSED': // Empty case falls through.
  case 'NOW_CLOSED':
    // Runs for both CLOSED and NOW_CLOSED.
    executeNowClosed();
    break;
}

// To implement fall-through in a non-empty case statement, use a continue statement with a label
var command = 'CLOSED';
switch (command) {
  case 'CLOSED':
    executeClosed();
    continue nowClosed;
  // Continues executing at the nowClosed label.

  nowClosed:
  case 'NOW_CLOSED':
    // Runs for both CLOSED and NOW_CLOSED.
    executeNowClosed();
    break;
}
Copy the code

Assertions (Assert)

In production code, assertions are ignored and passed parameters are not judged.

During development, you can interrupt code execution with assertion statements when a conditional expression is false.

If the value of the assertion expression is true, the assertion succeeds and execution continues. If the value of the expression is false, the assertion fails and an AssertionError is thrown.

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

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

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

The second argument adds a string message to it
assert(urlString.startsWith('https'),
    'URL ($urlString) should start with "https".');
Copy the code