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 valuesControl 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.