← Dart EnglishChapter 04 of 13

Control Flow

## Learning Objectives - Master if-else statements - Work with switch statements - Use loops effectively - Handle loop control with break and continue ## If-Else Statements ### Basic If ```dart void main() { int score = 85; if (score >= 90) { print('A'); } } ``` ### If-Else ```dart void main() { int score = 85; if (score >= 90) { print('A'); } else { print('B or lower'); } } ``` ### If-Else-If Chain ```dart void main() { int score = 85; if (score >= 90) { print('A'); } else if (score >= 80) { print('B'); } else if (score >= 70) { print('C'); } else { print('F'); } } ``` ### Ternary Operator ```dart void main() { int score = 85; String grade = score >= 60 ? 'Pass' : 'Fail'; print(grade); // Pass } ``` ### Nested If ```dart void main() { int age = 25; bool hasLicense = true; if (age >= 18) { if (hasLicense) { print('Can drive'); } else { print('Need a license'); } } else { print('Too young to drive'); } } ``` ## Switch Statements ### Basic Switch ```dart void main() { String grade = 'B'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; case 'C': print('Average'); break; default: print('Invalid grade'); } } ``` ### Switch with Multiple Cases ```dart void main() { String fruit = 'apple'; switch (fruit) { case 'apple': case 'orange': case 'banana': print('Common fruit'); break; case 'mango': case 'pineapple': print('Tropical fruit'); break; default: print('Unknown fruit'); } } ``` ### Switch Expression (Dart 3) ```dart void main() { String grade = 'B'; String result = switch (grade) { 'A' => 'Excellent', 'B' => 'Good', 'C' => 'Average', _ => 'Invalid grade', }; print(result); // Good } ``` ### Switch with Patterns (Dart 3) ```dart void main() { dynamic value = 42; switch (value) { case int n when n > 0: print('Positive integer: $n'); break; case int n when n < 0: print('Negative integer: $n'); break; case String s: print('String: $s'); break; default: print('Other'); } } ``` ## While Loops ### Basic While ```dart void main() { int count = 1; while (count <= 5) { print('Count: $count'); count++; } } ``` ### Do-While Loop ```dart void main() { int count = 1; do { print('Count: $count'); count++; } while (count <= 5); } ``` ### While vs Do-While ```dart void main() { // While: condition checked first int a = 10; while (a < 5) { print('while: a = $a'); // Never executes a++; } // Do-while: body executes at least once int b = 10; do { print('do-while: b = $b'); // Executes once b++; } while (b < 5); } ``` ## For Loops ### Traditional For Loop ```dart void main() { for (int i = 1; i <= 5; i++) { print('Iteration: $i'); } } ``` ### For-In Loop ```dart void main() { var items = ['apple', 'banana', 'orange']; for (var item in items) { print(item); } } ``` ### For-Each with Index ```dart void main() { var items = ['apple', 'banana', 'orange']; for (var i = 0; i < items.length; i++) { print('$i: ${items[i]}'); } // Using forEach items.asMap().forEach((index, value) { print('$index: $value'); }); } ``` ## Loop Control ### Break ```dart void main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exit loop entirely } print(i); // Prints 1, 2, 3, 4 } } ``` ### Continue ```dart void main() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip this iteration } print(i); // Prints 1, 2, 4, 5 } } ``` ### Labeled Breaks ```dart void main() { outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { break outerLoop; // Breaks outer loop } print('i=$i, j=$j'); } } } ``` ## Iterating Collections ### List ```dart void main() { var list = [1, 2, 3, 4, 5]; // forEach list.forEach((item) => print(item)); // for-in for (var item in list) { print(item); } } ``` ### Set ```dart void main() { var fruits = {'apple', 'banana', 'orange'}; for (var fruit in fruits) { print(fruit); } } ``` ### Map ```dart void main() { var ages = {'Alice': 30, 'Bob': 25}; // Iterate keys for (var key in ages.keys) { print('$key'); } // Iterate values for (var value in ages.values) { print('$value'); } // Iterate entries for (var entry in ages.entries) { print('${entry.key}: ${entry.value}'); } } ``` ## Summary - If-else: `if`, `else if`, `else` - Switch: `switch`, `case`, `break`, `default` (or `case _`) - Loops: `while`, `do-while`, `for` - Loop control: `break` (exit), `continue` (skip) - Use `for-in` for simple iteration over collections - Switch expressions (Dart 3) return values directly

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →