Async and Practical
FizzBuzz Rule Order
FizzBuzz is the classic test of else if ordering: % 15 has to be checked first, otherwise 15 would match % 3 and get labeled Fizz. The loop appends one label per value and a final join builds the deterministic summary.
Program
Play the program to label four numbers with FizzBuzz rules.
fizzbuzz.dart
void main() {
var values = [15, 3, 5, 16];
var labels = <String>[];
for (var n in values) {
if (n % 15 == 0) {
labels.add('FizzBuzz');
} else if (n % 3 == 0) {
labels.add('Fizz');
} else if (n % 5 == 0) {
labels.add('Buzz');
} else {
labels.add('$n');
}
}
print(labels.join(','));
}
rule order
`% 15` is checked first; otherwise `15` would match `% 3` and stop as `Fizz`.
else if chain
Only one arm runs per iteration; once a match wins, the rest are skipped.
collect then join
Appending to `labels` and joining once at the end keeps the final string deterministic.