Last active
January 23, 2020 17:40
-
-
Save AlexVegner/b68bfb6f6574132c3d7792c00c26d847 to your computer and use it in GitHub Desktop.
dart_control_flow_statements.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // ******* | |
| // Conditional statement | |
| // ******* | |
| // example: value mapping | |
| // '1' = 'yes' | |
| // '0' = 'no' | |
| // in other case = 'undefined' | |
| String value = '1'; | |
| // Conditional expressions (ternary operator) | |
| String result = value == '1' ? 'yes' : (value == '0' ? 'no' : 'undefined'); | |
| print(result); | |
| // if-else | |
| if (value == '1') { | |
| result = 'yes'; | |
| } else if (value == '0') { | |
| result = 'no'; | |
| } else { | |
| result = 'undefined'; | |
| } | |
| print(result); | |
| // Switch-case works with: int, String, enum | |
| switch (value) { | |
| case '1': | |
| result = 'yes'; | |
| break; | |
| case '0': | |
| result = 'no'; | |
| break; | |
| default: | |
| result = 'undefined'; | |
| } | |
| print(result); | |
| // ******* | |
| // Loops | |
| // ******* | |
| // Infinite loops | |
| // for(;;) { } | |
| // while (true) { } | |
| // do {} while (true); | |
| var list = [1, 2, 3, 4]; | |
| print(''); // new line | |
| // for | |
| for (var i = 0; i < list.length; i++) { | |
| if (list[i] == 1) { | |
| continue; // skips current iteration | |
| } | |
| if (list[i] == 4) { | |
| break; // exit from the loop | |
| } | |
| print(list[i]); | |
| } | |
| print(''); // new line | |
| int i = 6; | |
| // while | |
| while (i < 10) { i++; } | |
| int e = 8; | |
| // do-while | |
| do { e++; } while (e < 10); | |
| // for-in | |
| for (var s in list) { | |
| // print(s); | |
| } | |
| // for-each | |
| list.forEach((e) => print(e)); | |
| // continue и break statements also works with: while, do-while and for-in | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment