Loops
This section describes how to create looping code.
Loop Keyword
Section titled “Loop Keyword”The “loop” keyword can be used to run a block of code in an infinite loop or a certain number of times.
// infinite loop loop { println("weeeeee!"); // warning, this will print FOREVER }
// only loop 5 times loop 5 { println("hello"); // print "hello" 5 times }
// math and variables can be used as counters. i32 x = 2; i32 y = 4; loop x + y { println("hi"); // print "hi" six times. }For Loop
Section titled “For Loop”The “for” loop runs a limited number of times by iterating over a number, range, or collection.
This type of loop includes a variable that gets populated each time through the loop.
// for (variable_name) in (number) { ... } for i in 5 { // loops 5 times --> exactly the same as: loop 5 { ... } println("hello"); }The variable in the for loop signature can be used inside the { } block. It starts at 0 and gets a +1 at the end of each loop cycle.
// This for loop will print 0, 1, 2, and 3 on separate lines. for i in 4 { // loop 4 times with i starting at 0. println(i); }
// This for loop will print 0, 1, 4, and 9 by squaring the number stored in i. for i in 4 { println(i * i); }
// You can also change the counter from inside the loop. // This loop will start with i = 0 and stop when i >= 5. for i in 5 { println(i); // prints 0, 2, 4 on separate lines. i += 1; // add an extra +1 each time through the loop. }If you don’t need the variable, it’s good practice to name it ”_” so it’s obvious. The ”_” is a special variable that cannot be read or written to.
for _ in 4 { // exactly the same as: loop 4 { ... } println("hi"); }Ranges
Section titled “Ranges”If you want to control the starting and stopping values in the counter you can use a Range.
The range stopping location can be exclusive or inclusive:
Exclusive Stop: start..stopInclusive Stop: start..=stopIn math notation exclusive is written as [start, stop). This means that the range goes up to the stop number but it does not include it. For example, the range 0..5 is the same as [0, 5), which are the numbers 0, 1, 2, 3, and 4.
Inclusive in math notation is written as [start, stop]. This means that the range goes up to and includes the stop number. For example, the range 5..=8 is the same as [5, 8], which are the numbers 5, 6, 7, 8.
Both forms can be useful in different situations. Here are a couple examples in for loops.
for i in 0..3 { println(i); // prints 0, 1, 2 on separate lines. }
for i in 0..=3 { println(i); // prints 0, 1, 2, 3 on separate lines. }
// math and variables can be used in ranges i32 x = 2; i32 y = 4; for i in x..=(x + y) { // same as for i in 2..=6 println(i); // prints 2, 3, 4, 5, 6 on separate lines. }While Loop
Section titled “While Loop”A “while” loop uses conditional logic to determine when to stop.
i32 i; // i = 0 by default. while i < 10 { // loop as long as i is less than 10. i = i + 1; // add +1 to i each time through the loop. }
while false { // condition is never true println("hello"); // this code will never run! }
// infinite while loop, same as: loop { ... } while true { println("weeeeee!"); // warning, this will print FOREVER }Break and Continue
Section titled “Break and Continue”The “break” statement is used when you want to force exit a loop.
for i in 7 { if i > 3 { // exit as soon as i is greater than 3 break; } println(i); // only prints 0, 1, 2, 3 on separate lines. }
i32 i; loop { println(i); // only prints 0, 1, 2 on separate lines i += 1; if i > 2 { break; } // exit as soon as i is greater than 2. }The “continue” statement is used when you want to skip the remaining code in a loop and immediately start the next cycle.
for i in 7 { if i < 4 { // skip any cycle where i is less than 4. continue; } println(i); // only prints 4, 5, 6 on separate lines. }
i32 i; string output; while i < 5 { i += 1; output += "ba"; // every cycle add: ba if i % 2 == 0 { output += "-"; continue; // skip the rest if i is an even number. } output += "rf! "; // every-other-cycle add: rf } println(output); // "barf! ba-barf! ba-barf! "Note: break and continue only work for the current loop, not for loops inside of loops.
Loop Labels
Section titled “Loop Labels”// add break/continue at label
Iterating Collections
Section titled “Iterating Collections”A “for” loop can also iterate over collections. See examples in: Vec Array, Map, and Set.