Changing the execution flow of a program
Dart has the usual control structures with no surprises here (refer to control.dart
).
An if...else
statement (with an optional else
) is as follows:
var n = 25; if (n < 10) { print('1 digit number: $n'); } else if (n >= 10 && n < 100){ print('2+ digit number: $n'); // 2+ digit number: 25 } else { print('3 or more digit number: $n'); }
Single-line statements without {}
are allowed, but don't mix the two. A simple and short if…else
statement can be replaced by a ternary operator, as shown in the following example code:
num rabbitCount = 16758; (rabbitCount > 20000) ? print('enough for this year!') : print('breed on!'); // breed on!
If the expression before ?
is true, the first statement is executed, else the statement after :
is executed. To test if a variable v
refers to a real object, use: if (v != null) { … }
.
Testing if an object v
is of type T
is done with an if
statement: if (v is T)
.
In that case we can safely cast v
to type T
and access all members of T
:
if (v is T) { (v as T).methodOfT() }
For example, if we don't know for sure that ba2
is a BankAccount
, the code in line (1)
in the following code will generate an error; we can avoid this with an if
test in line (2)
:
var ba1, ba2; ba1 = new BankAccount("Jeff", "5768-346-89", 758.0); if (ba1 is BankAccount) ba1.deposit(42.0); print('${ba1.balance}'); // 800.0 (ba2 as BankAccount).deposit(100.0); <-- NoSuchMethodError (1) if (ba2 is BankAccount) { (2) (ba2 as BankAccount).deposit(100.0); print('deposited 100 on ba2'); // statement not reached } else { print('ba2 is not a BankAccount'); // ba2 is not a BankAccount }
We can replace multiple if...else if
with a switch case statement; switch tests the value of an integer or string variable in ()
against different constant values in case clauses:
switch(ba1.owner) {
case 'Jeff':
print('Jeff is the bank account owner'); // this is printed
break
;
case 'Mary':
print('Mary is the bank account owner');
break;
default:
print('The bank account owner is not Jeff, nor Mary');
}
Each case must end with a break
or a continue
with a label; use default
when no other case matches; multiple cases can be combined.
Repetition can be coded with a for
loop if the number of repetitions is known or with a while
or do...while
loop if the looping depends on a condition:
var langs = ["Java","Python","Ruby", "Dart"]; for (int i = 0; i < langs.length; i++) { print('${langs[i]}'); }
Notice that the condition i
value should be less than the length of the List.
If you don't need the index i
, the for...in
loop provides a simpler alternative:
var s = ''; var numbers = [0, 1, 2, 3, 4, 5, 6, 7]; for (var n in numbers) { s = '$s$n '; } print(s); // 0 1 2 3 4 5 6 7
In each loop the variable n
takes the value of the next collection element.
Conditions without counters are best tested in a while
loop:
while (rabbitCount <= 20000) { print('keep breeding'); rabbitCount += 4; }
Don't get involved in an infinite loop by forgetting a statement that changes the condition! You can always break out from a loop with a break
:
while (true) { if (rabbitCount > 20000) break; rabbitCount += 4; }
Likewise, skip the execution of the body of the loop with a continue
:
s = ''; for (var n in numbers) { if (n % 2 == 0) continue; // skip even numbers s = '$s$n '; } print('$s'); // 1 3 5 7