Dart Tutorial

Control Flow

if/else, switch, and loops in Dart

Conditional Logic (if)

In Dart, if statements work just like in other C-style languages.
dart
if (isReady) {
  print('Go!');
} else {
  print('Wait...');
}

The Modern switch (Dart 3.0+)

Wait! Dart 3.0 introduced a massively upgraded Switch system. It can now be used as an expression (returning a value) and supports pattern matching.
dart
var status = switch (statusCode) {
  200 => 'Success',
  404 => 'Not Found',
  _ => 'Unknown' // The default case
};

Loops in Dart

Dart provides the standard loops you expect:
  1. for: Best for a fixed number of iterations.
  2. for-in: The easiest way to iterate through a list.
  3. while: Runs as long as a condition is true.
dart
var fruits = ['Apple', 'Banana', 'Cherry'];

for (var fruit in fruits) {
  print(fruit);
}

Assertions

During development, you can use assert to verify that a condition is true. Assertions are ignored in production, so they have no performance cost for your users.
dart
assert(age >= 18, 'Must be an adult');

Summary

KeywordDescription
if / elseBasic branching
switchMulti-way branch (supports patterns in Dart 3+)
for-inIterating through collections
whileBasic loop with condition
breakExit loop immediately
continueSkip current iteration
LogicEssential for app logic and UI states
SafetySwitches must be exhaustive if using Enums
ModernSwitch expressions and patterns make code very concise
StandardUse for-in for almost all collection processing
IdentityControl flow returns no value except in switch expressions