Page 24 - Dart Language
P. 24

}



        For Loop


        Two types of for loops are allowed:


         for (int month = 1; month <= 12; month++) {
           print(month);
         }


        and:


         for (var object in flybyObjects) {
           print(object);
         }


        The for-in loop is convenient when simply iterating over an Iterable collection. There is also a
        forEach method that you can call on Iterable objects that behaves like for-in:


         flybyObjects.forEach((object) => print(object));


        or, more concisely:


         flybyObjects.forEach(print);


        Switch Case


        Dart has a switch case which can be used instead of long if-else statements:


         var command = 'OPEN';

         switch (command) {
           case 'CLOSED':
             executeClosed();
             break;
           case 'OPEN':
             executeOpen();
             break;
           case 'APPROVED':
             executeApproved();
             break;
           case 'UNSURE':
             // missing break statement means this case will fall through
             // to the next statement, in this case the default case
           default:
             executeUnknown();
         }


        You can only compare integer, string, or compile-time constants. The compared objects must be
        instances of the same class (and not of any of its subtypes), and the class must not override ==.





        https://riptutorial.com/                                                                               19
   19   20   21   22   23   24   25   26   27   28   29