for
Use for statements to loop over a statement for a specified number of values.
General Form
for (controlVariable in values) { stmt; }
where ''values'' can be:
a simple integer range defined like:
first..last
or an iterable data collection like:
- sequence
- set
- enum
- map
Examples
{ for (i in 1..10) { pln(#i); } }
{ Object[] seq(); seq << "Hello" << "World" << Int(8) << Double(2.3) << "Dude"; for (o in seq) { pln(#o); } }
{ str->int nameToAge(); nameToAge.put("Joan", 41); nameToAge.put("Mike", 32); nameToAge.put("John", 55); for (n, a in nameToAge) { pln(n, " is ", a, " years old"); } }
Keywords
index
The value of the current index of the loop.
{ for (n in 1..5, index=i) { pln(#i; #n); } }
Output: i=0, n=1 i=1, n=2 i=2, n=3 i=3, n=4 i=4, n=5
{ int[] values(); values << 3 << 8 << 10 << 31 << 100; for (n in values, index=i) { pln(#i; #n); } }
Output: i=0, n=3 i=1, n=8 i=2, n=10 i=3, n=31 i=4, n=100
step
Iterate in steps. Applicable for numerics.
{ for (n in 1..5, index=i, step=2) { pln(#i; #n); } }
Output: i=0, n=1 i=1, n=3 i=2, n=5
{ for (d in 1..1.74, step=.2) { pln(#d); } }
Output: d=1 d=1.2 d=1.4 d=1.6
reverse
Iterate backward. Applicable for sequences, sorted maps, and strings.
{ int[] values(); values << 3 << 8 << 10 << 31 << 100; for (n in values, index=i, reverse) { pln(#i; #n); } }
Output: i=4, n=100 i=3, n=31 i=2, n=10 i=1, n=8 i=0, n=3
start & end
Start and end iteration at the given index. Applicable for sequences and strings.
private str txt = "A simple text";
{ for (s in txt, start=9) { pln(s); } }
Output: t e x t
{ for (s in txt, end=7) { pln(s); } }
Output: A s i m p l e
{ for (s in txt, start=2, end=7) { pln(s); } }
Output: s i m p l e
{ for (s in txt, start=7, end=2, reverse) { pln(s); } }
Output: e l p m i s
Keywords Inside Loop
Some boolean keywords exist for convenience during iteration.
more
Is true if there are more values, i.e. if the loop will iterate at least one more time.
{ str[] lines(); lines << "First line" << "Second line" << "Third line"; StrBuf buf(); for (l in lines) { buf << l; if (more) buf << "\n"; } pln(buf); }
Output: First line Second line Third line
first
Is true if this is the first iteration.
{ point ref(1, 1, 1); point{} allPositions(); allPositions << (0, 0, 0) << (1, 0, 1) << (0, 2, 1); point closest; for (p in allPositions) { if (first) { closest = p; } else { if (ref.distance(p) < ref.distance(closest)) { closest = p; } } } }
last
Is true if this is the last iteration.
{ str[] linesWLF(); linesWLF << "First line\n" << "Second line\n" << "Third line\n"; StrBuf buf(); for (l in linesWLF) { buf << l; if (last) buf.dropLast(); } pln(buf); }
Output: First line Second line Third line
while
Use while statements to loop over a statement as long as a specified condition is fulfilled.
General Form
while (condition) { stmt; }
Another possibility is to include initialization and/or increment in the while statement.
while (initialization; condition; increment) { stmt; }
Examples
{ int i = 0; while (i < 100) { pln(#i); i++; } }
{ while (int i = 0; i <= 10; i++) { pln(#i); } }
{ int maxLength = 1024; int count; StrBuf txt(); Url url("c:\\temp\\testFile.txt"); if (url.isReadable) { if (File input = openForRead(url)) { pln("Opened file"); while (input.more) { count++; if (count > maxLength) { pln("Stopped reading"); break; } char next = input.get().char; txt << next; } input.close(); pln("Read ", txt.count, " characters from ", url); } } else { pln("Not readable: ", url); } }
do..while
A do while-loop executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block.
General Form
do { stmt; } while condition
Example
{ int i = 0; do { pln(#i); } while (i++ < 10); }
Comments
0 comments
Please sign in to leave a comment.