Java Reference
In-Depth Information
var taxBracket =
if
(income < 8025.0) 0.10
else if
(income < 32550.0)0.15
else if
(income < 78850.0) 0.25
else if
(income < 164550.0) 0.28
else
0.33;
For
for
loops are used with sequences and allow you to iterate over the members of
a sequence.
var daysOfWeek : String[] =
[ "Sunday", "Monday", "Tuesday" ];
for(day in daysOfWeek) {
println("{indexof day}). {day}");
}
To be similar with traditional
for
loops that iterate over a count, use an integer
sequence range defined within square brackets.
for( i in [0..100]} {
The
for
expression can also return a new sequence. For each iteration, if the
expression block executed evaluates to an
Object
, that
Object
is inserted into a
new sequence returned by the
for
expression. For example, in the following
for
expression, a new
Text
node is created with each iteration of the day of the
week. The overall
for
expression returns a new sequence containing
Text
graph-
ical elements, one for each day of the week.
var textNodes: Text[] = for( day in daysOfWeek) {
Text {content: day };
}
Another feature of the
for
expression is that it can do nested loops. Listing 3.8
shows an example of using nested loops.
Listing 3.8
Nested For Loop
class Course {
var title: String;
var students: String[];
}
var courses = [
Search WWH ::
Custom Search