Loops and ranges
for over ranges, arrays and slices, as a while, and forever; range values.
One loop keyword
for is the only loop. It can:
- go over a range, an array or a slice;
- run while a condition holds;
- run until
break.
continue goes on to the next pass.
for i, v in 10..13 { // the pass number, from 0, and the value
for n < 100 { // while `n < 100`
for { // until `break`// Loops: `for` is the only loop. It goes over a range, an array or a slice,
// runs while a condition holds, or runs until `break`. `continue` goes on
// to the next pass.
// Run: peeper run examples/06_loops.peep
fn main() {
print("range:");
for i in 0..5 {
print("", i);
}
println();
print("numbered:");
for i, v in 10..13 { // the pass number, from 0, and the value
print("", i, v);
}
println();
let mut passes = 0;
for 0..3 { // no name for the value
passes = passes + 1;
}
println("passes:", passes);
let mut n = 1;
for n < 100 { // while `n < 100`
n = n * 3;
}
println("while:", n);
let mut x = 0;
for { // until `break`
x = x + 1;
if x == 4 {
break;
}
}
println("forever:", x);
print("odd:");
for v in 0..10 {
if v % 2 == 0 {
continue;
}
if v > 7 {
break;
}
print("", v);
}
println();
}range: 0 1 2 3 4
numbered: 0 10 1 11 2 12
passes: 3
while: 243
forever: 4
odd: 1 3 5 7Ranges
a..bcounts fromaup to, but not including,b.a..=bincludesb.:stepsteps by more than 1.
A range counts down when a is larger than b, and it never wraps past the
end of its type.
A range is a value of type range[T]. You can keep it in a variable, pass
it, return it and copy it, like a number. values... in an array literal
gives each of a range's values.
fn evens(below: i32) -> range[i32] {
return 0..below:2;
}// Ranges: `a..b` counts from `a` up to `b`, `a..=b` takes `b` too, and
// `:step` steps by more than 1. A range counts down when `a > b`, and never
// wraps past its type's end. A range is a value of type `range[T]`: it is
// kept, passed, returned and copied like a number.
// Run: peeper run examples/07_ranges.peep
struct Window { values: range[u8], tag: i32 }
fn evens(below: i32) -> range[i32] {
return 0..below:2;
}
fn total(values: range[i32]) -> i32 {
let mut sum = 0;
for v in values {
sum = sum + v;
}
return sum;
}
fn main() {
print("up:");
for v in 0..5 { print("", v); }
println();
print("inclusive:");
for v in 0..=5 { print("", v); }
println();
print("step:");
for v in 0..10:3 { print("", v); }
println();
print("down:");
for v in 5..=0:2 { print("", v); }
println();
print("top of u8:");
for v in 250u8..=255 { print("", v); }
println();
let r = 1..=4;
let copy = r;
println("totals:", total(r), total(copy), total(evens(9)));
let window = Window.{ values = 10..=20:5, tag = 1 };
print("field:");
for v in window.values { print("", v); }
println(" tag", window.tag);
println("spread:", [5]i32{(0..4)..., 100}); // a range's values in an array
}up: 0 1 2 3 4
inclusive: 0 1 2 3 4 5
step: 0 3 6 9
down: 5 3 1
top of u8: 250 251 252 253 254 255
totals: 10 10 20
field: 10 15 20 tag 1
spread: [0, 1, 2, 3, 100]