Skip to the workspace
Pebellslearn by building
Basics of Dart  ›  Capstone Project

Operators and Control Flow

Control Flow Statements 15 minOperators such as integer division and the null-aware operators compute values, while if, loops and switch decide which statements run.

flutter / Operators and Control Flow

Explore each scene at your own pace.

01 · The rule

The rule

Operators and Control Flow

Every morning Sprout must answer one question: which plants need water today? That takes both halves of this lesson. Operators compute values like "days since watering", and control flow decides which lines run for each plant.

The rule

Operators make a value; control flow chooses what runs next.

Operators worth knowing early

7 / 2     // 3.5  — division always gives a double
7 ~/ 2    // 3    — integer division drops the remainder
7 % 2     // 1    — the remainder
days >= every            // comparison, gives a bool
name ?? 'Unnamed'        // if name is null, use 'Unnamed'
plant?.name              // if plant is null, the result is null instead of a crash
days ??= 7;              // assign 7 only if days is null
days > 3 ? 'later' : 'soon'   // choose between two values

Control flow

for (final p in plants) {
  if (p.daysSince >= p.every) {
    print('${p.name} is due');
  } else if (p.daysSince == p.every - 1) {
    print('${p.name} is due tomorrow');
  }
}

final label = switch (plant.every) {
  1 => 'daily',
  7 => 'weekly',
  _ => 'every ${plant.every} days',
};

for-in walks a collection, while repeats until a condition is false, and a switch picks one branch. In Dart 3 a switch expression produces a value, _ matches anything left, and a case with a body never falls through into the next one.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.