Control Flow Statements
Control flow statements determine the order in which code executes. Dart provides various control structures for decision-making and iteration.
Conditional Statements
if-else
Conditional Expressions
Ternary Operator
Null-aware Operator
switch-case
Loops
for Loop
while Loop
do-while Loop
Loop Control
break
Exits the loop immediately:
continue
Skips the current iteration:
Assert
Used for debugging to ensure conditions are true:
Pattern Matching (Dart 3.0+)
Switch Expressions
if-case
Collection Control Flow
Collection if
Collection for
Spread Operator
Best Practices
- Use switch expressions for cleaner code (Dart 3.0+)
- Prefer for-in over traditional for loops when iterating collections
- Use collection if/for for conditional list building
- Avoid deep nesting of if-else statements
- Use early returns to reduce nesting